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

How do I configure Vim's :grep command to use ripgrep for faster project-wide searching?

Answer

:set grepprg=rg\ --vimgrep\ --smart-case

Explanation

By default, Vim's :grep command calls the system grep. Setting grepprg replaces it with any external tool — most commonly ripgrep (rg), which is far faster for large codebases and respects .gitignore by default. Combined with the correct grepformat, :grep sends results straight to the quickfix list just like :vimgrep, but at native speed.

How it works

Add to your vimrc:

if executable('rg')
  set grepprg=rg\ --vimgrep\ --smart-case
  set grepformat=%f:%l:%c:%m
endif
  • grepprg — the external command :grep will call; backslash-escaped spaces are literal
  • --vimgrep — outputs file:line:col:match format that Vim can parse
  • --smart-case — case-insensitive unless the pattern contains uppercase
  • grepformat — teaches Vim to parse the file:line:col:message output format

Example

Search for all usages of MyFunction across the project:

:grep MyFunction .
:copen

The quickfix window shows every match with file, line, and column. Navigate with :cn / :cp or ]q / [q.

Tips

  • Replace rg with ag (The Silver Searcher), fd, or any tool that accepts a pattern and file glob.
  • Use :grep! to run without jumping to the first result: :grep! pattern . | copen
  • Set grepformat=%f:%l:%c:%m (with column) or %f:%l:%m (without) to match your tool's output.
  • grepprg and grepformat can also be set per-project via a local .vimrc with exrc.

Next

How do I prepend a value to the beginning of a comma-separated Vim option like path or wildignore?