vimtricks.wiki Concise Vim tricks, one at a time.

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 :grep receives.
  • grepformat — a scanf-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 .vimrc or init.vim to make it permanent
  • The default grepprg is grep -n $* /dev/null on Unix — functional but slower than modern tools
  • grepformat can 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 | update to do project-wide search-and-replace via the quickfix list

Next

How do I open just enough folds to see the current line without expanding everything?