Using the silver searcher with Vim

Note, this article was published over 2 years ago and hence the content may be stale. Consume with a pinch of salt.

If you're not doing this already, then you should use the Silver Searcher within Vim for rapid, convenient file searching. In a nutshell, ag offers similar functionality to ack but with much better performance.

It's easily installed - on OSX, run:

$ brew install the_silver_searcher

Urge Vim to use it for :grep commands by adding the following to ~/.vimrc:

if executable('ag')
    " Note we extract the column as well as the file and line number
    set grepprg=ag\ --nogroup\ --nocolor\ --column
    set grepformat=%f:%l:%c%m

:grep searches are now lightning-fast and respectful of your ~/.gitignore patterns. It's genuinely impressive.

Improve your efficiency further by remapping the keys for jumping through search matches (stored in the "quickfix" list):

nmap <silent> <RIGHT> :cnext<CR>
nmap <silent> <LEFT> :cprev<CR>

I'm using the cursor keys since I normally have them disabled.

See also: