How do I configure Vim's :grep command to use a faster external search tool like ripgrep or ag?
Answer
:set grepprg={cmd} grepformat={fmt}
Explanation
Vim's :grep command delegates to an external tool defined by grepprg, then parses the output according to grepformat to populate the quickfix list. By customizing both, you can use any grep-compatible tool — ripgrep, ag, git grep — and navigate results with :cnext/:cprev as usual.
How it works
grepprg— the command template. Use$*as a placeholder for the search arguments:grepreceives.grepformat— ascanf-style format string telling Vim how to parse each output line into file, line, column, and message.
Common format tokens: %f (file), %l (line), %c (column), %m (message), %\s (whitespace).
Example
Configure for ripgrep (produces file:line:col:match output):
:set grepprg=rg\ --vimgrep\ --smart-case
:set grepformat=%f:%l:%c:%m
Configure for git grep (shows results only within the repo):
:set grepprg=git\ grep\ -n\ $*
:set grepformat=%f:%l:%m
Then use :grep as usual and results populate the quickfix list:
:grep 'MyFunction' **/*.go
:copen
Tips
- Add to
.vimrcorinit.vimto make it permanent - The default
grepprgisgrep -n $* /dev/nullon Unix — functional but slower than modern tools grepformatcan list multiple formats separated by commas — Vim tries each one- To search the current file only:
:grep 'pattern' % - Pair with
:cfdo %s/old/new/g | updateto do project-wide search-and-replace via the quickfix list