The Silver Searcher - A Better Way to Grep

I've been using Silver Searcher for a while now, but got inspired to write a little blurb after watching Gary Bernhardt use it in one of his screencasts.

Vim_image

The Silver Searcher is a faster code searching tool and than Ack. It's written in C and uses pthreads to take advantage of the multiple CPU cores on your machine to run searches in parallel.

An example use case is to look in the /spec and /lib directories of a rails application.

Ag 'something to search' --ruby spec/ lib/

Sample output

Prepend the search term with -L to list only the files

Ag -L 'something to search' --ruby spec

Sample Output:

Open all files that match in vim

vim `Ag -L 'something to search' --ruby spec`

So to make this even more useful, add it to your dotfiles / vimrc and use it anytime you search.

if executable('ag')
  " Use Ag over Grep
  set grepprg=ag\ --nogroup\ --nocolor

  " Use ag in CtrlP for listing files. Lightning fast and respects .gitignore
  let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'

  " ag is fast enough that CtrlP doesn't need to cache
  let g:ctrlp_use_caching = 0
endif

" bind \ (backward slash) to grep shortcut
command! -nargs=+ -complete=file -bar Ag silent! grep! <args>|cwindow|redraw!
nnoremap \ :Ag<SPACE>
" bind F to grep word under cursor
nnoremap F :grep! "\b<C-R><C-W>\b"<CR>:cw<CR>

For more reading, check out Thoughtbot's overview - it's where I got my dotfiles :).