Windows Cygwin環境のThe Silver Searcher(ag.exe)ビルド方法
grepに変わるack、、、に変わるagとして最近話題のagについての紹介です。
The Silver Searcherの特徴
公式に書いてあるThe Silver Searcherの特徴
ackの3〜5倍高速
ackを捨てて、より高速なag(The Silver Searcher)に切り替えた - Glide Note - グライドノート
.gitignore、.hgignoreに記載されているものを検索対象から除外
検索対象から除外したいファイルは.agignoreに記載
agというコマンド名で、ackと比べてコマンドが短い(33%減!)
私も早速Linux環境に導入してみましたが、打ちやすいコマンドと検索スピードの虜になってしまいました。
そうなるとWindowsでもagが使いたくなるということで、Windows向けビルド手順を調べてみました。
しかしいくつかagのビルド方法が公開されているのですが最新のコミットではうまくビルドできないなど、ビルド手順が確立されていないようなのでまとめてみました。
ビルドがうまくいかない方は./build.sh実行後に以下のようなエラーが出ているかと思います。
src/decompress.c: In function `decompress_lzma':
src/decompress.c:113: error: `lzma_stream' undeclared
checking pkg-config is at least version 0.9.0... yes
checking for PCRE... yes
checking for LZMA... configure: error: Package requirements (liblzma) were not met:No package 'liblzma' found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.Alternatively, you may set the environment variables LZMA_CFLAGS
and LZMA_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
lzma関連のパッケージを正しくインストールすると解決しますが、以降の解説ではビルド手順を最初から説明します。
必要なパッケージのインストール
Cgywinで以下のパッケージをすべてインストールしましょう。
gcc make autoconf automake
zlib-devel pkg-config
pcre pcre-devel libpcre libpcre-devel lzma liblzma-devel
git
- apt-cygのインストール方法
setup.exeからインストール可能ですが、apt-cygコマンドを使うとカンタンに環境を整えることができます。
cd /usr/local/bin wget http://apt-cyg.googlecode.com/svn/trunk/apt-cyg chmod 755 apt-cyg
- apt-cygを使う場合は以下のコマンドで
apt-cyg install gcc make autoconf automake zlib-devel pkg-config pcre pcre-devel libpcre libpcre-devel lzma liblzma-devel lzma liblzma-devel git
the Silver Searcherのビルド
mkdir /usr/local/src cd !$ git clone https://github.com/ggreer/the_silver_searcher.git cd the_silver_searcher make -f Makefile.w32
the Silver Searcherの配置
$ make install $ which ag /usr/local/bin/ag
以上で完了です。
使い方
よく使うような-wや-iなどはgrepと同じように使うことができますが、agコマンドではデフォルトでカレントディレクトリ以下の再起探索(./ -r)とカラー(--color)が有効になっています。
またagは同じファイル内の検索結果はまとめて表示されます。(--group)
コマンドで表すと以下の2つが同じような出力結果となります。
grep SEARCH_WORD ./ -r --color ag SEARCH_WORD --nogroup
普段はこれだけのコマンドで目的の検索ができてしまいます。
ag SEARCH_WORD
詳細についてはag --helpで確認してみてください。
$ ag Usage: ag [OPTIONS] PATTERN [PATH] Recursively search for PATTERN in PATH. Like grep or ack, but faster. Example: ag -i foo /bar/ Search options: --ackmate Print results in AckMate-parseable format -a --all-types Search all files (doesn't include hidden files or patterns from ignore files) -A --after [LINES] Print lines before match (Default: 2) -B --before [LINES] Print lines after match (Default: 2) --[no]break Print newlines between matches in different files (Enabled by default) --[no]color Print color codes in results (Enabled by default) --color-line-number Color codes for line numbers (Default: 1;33) --color-match Color codes for result match numbers (Default: 30;43) --color-path Color codes for path names (Default: 1;32) --column Print column numbers in results --line-numbers Print line numbers even for streams -C --context [LINES] Print lines before and after matches (Default: 2) -D --debug Ridiculous debugging (probably not useful) --depth NUM Search up to NUM directories deep (Default: 25) -f --follow Follow symlinks --[no]group Same as --[no]break --[no]heading -g PATTERN Print filenames matching PATTERN -G, --file-search-regex PATTERN Limit search to filenames matching PATTERN --[no]heading --hidden Search hidden files (obeys .*ignore files) -i, --ignore-case Match case insensitively --ignore PATTERN Ignore files/directories matching PATTERN (literal file/directory names also allowed) --ignore-dir NAME Alias for --ignore for compatibility with ack. -l --files-with-matches Only print filenames that contain matches (don't print the matching lines) -L --files-without-matches Only print filenames that don't contain matches -m --max-count NUM Skip the rest of a file after NUM matches (Default: 10,000) --no-numbers Don't show line numbers -p --path-to-agignore STRING Use .agignore file at STRING --print-long-lines Print matches on very long lines (Default: >2k characters) -Q --literal Don't parse PATTERN as a regular expression -s --case-sensitive Match case sensitively (Enabled by default) -S --smart-case Match case insensitively unless PATTERN contains uppercase characters --search-binary Search binary files for matches --stats Print stats (files scanned, time taken, etc.) -t --all-text Search all text files (doesn't include hidden files) -u --unrestricted Search all files (ignore .agignore, .gitignore, etc.; searches binary and hidden files as well) -U --skip-vcs-ignores Ignore VCS ignore files (.gitigore, .hgignore, .svnignore; still obey .agignore) -v --invert-match -w --word-regexp Only match whole words -z --search-zip Search contents of compressed (e.g., gzip) files
参考
ggreer/the_silver_searcher · GitHub
https://github.com/ggreer/the_silver_searcher
MinGWでSilver Searcher(ag.exe)をビルドする方法。 #Windows #MinGW - Qiita [キータ]
http://qiita.com/items/7d7a4962cdac878d7471
2013-03-25 [emacs] The Silver Searcher のビルド手順 for gnupackAdd Star
http://d.hatena.ne.jp/zqwell-ss/20130325/1364232704