Using the silver searcher with Vim

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
endif

: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:

——————

Something wrong? Suggest an improvement or add a comment (see article history)
Tagged with: vim
Filed in: tips

Previous: The road to Oscar 1.0
Next: Continuously rebuild your project

Copyright © 2005-2022 David Winterbottom
Content licensed under CC BY-NC-SA 4.0.