LoginSignup
137
135

More than 5 years have passed since last update.

agでvimの検索関連を高速化

Last updated at Posted at 2014-05-07

agがとにかく高速で検索できるそうなので今更試してみた。
そしたらすごく速くなった!

ag のインストール

公式の手順が確実です。
https://github.com/ggreer/the_silver_searcher

Mac

$ brew install ag

CentOs

$ rpm -ivh http://swiftsignal.com/packages/centos/6/x86_64/the-silver-searcher-0.14-1.el6.x86_64.rpm
# or 
$ rpm -ivh http://swiftsignal.com/packages/centos/6/i386/the-silver-searcher-0.14-1.el6.i686.rpm

Windows

手順通りソースからビルドするか、以下からバイナリを利用させていただく。

The Silver Searcher windows port
http://blog.kowalczyk.info/software/the-silver-searcher-for-windows.html

コマンドから利用

$ ag hoge

オプション(一部)

オプション 意味
-a 無視リストも対象
-u 隠しファイルおよび無視リストも対象
-g ファイル名で検索
-G 検索対象の指定 [ag -G *.css]
-l 一致したファイル名のみ
-L 一致していないファイル名のみ
-v 不一致検索
-Q 正規表現を無視
-z 圧縮ファイルも対象

無視ファイルリスト

.agignoreで指定可能

ag の検索で除外設定を使う - Qiita

vimで利用

ag.vim

NeoBundle 'rking/ag.vim'

コマンド

コマンド 結果
:Ag カレントディレクトリを再帰的に検索して quickfix へ
:AgAdd :Ag同様だが結果を追加
:AgFromSearch 前回の検索パターンを使用(/による検索など)
:LAg :Ag同様だが、結果を location-list へ
:LAgAdd :AgAdd同様だが、結果を location-list へ
:AgFile ファイル名の検索
:AgHelp vimのドキュメントが検索対象

quickfixでの操作

コマンド 内容
e 開くと同時に終了
o 開く
go プレビュー
t タブで開く
T タブで開く(silently)
h 水平分割で開く
H 水平分割で開く(silently)
v 垂直分割で開く
gv 垂直分割で開く(silently)
q 終了

結果

速い!

Unite.vim と組み合わせる

Unite.vimはインストール済みとして。

" カーソル位置の単語をgrep検索
nnoremap <silent> ,cg :<C-u>Unite grep:. -buffer-name=search-buffer<CR><C-R><C-W>

" unite grep に ag(The Silver Searcher) を使う
if executable('ag')
  let g:unite_source_grep_command = 'ag'
  let g:unite_source_grep_default_opts = '--nogroup --nocolor --column'
  let g:unite_source_grep_recursive_opt = ''
endif

参考:agとUnite.vimで快適高速grep環境を手に入れる - Thinking-megane

結果

速い!

ctrlp.vim と組み合わせる

ctrlp.vimはインストール済みとして。

let g:ctrlp_user_command = 'ag %s -l'

結果

速い!

137
135
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
137
135