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

How do I configure Vim to use ripgrep as its built-in grep program?

Answer

:set grepprg=rg\ --vimgrep

Explanation

Vim's :grep command uses an external program to search files and populate the quickfix list. By changing grepprg to use ripgrep (rg), you get dramatically faster searches with smart defaults like respecting .gitignore and skipping binary files.

How it works

  • grepprg — the external program Vim uses for :grep
  • grepformat — how to parse the program's output into quickfix entries
  • rg --vimgrep — outputs in the format Vim expects (file:line:col:match)
  • :grep pattern then uses ripgrep instead of system grep

Example

" In your vimrc
set grepprg=rg\ --vimgrep
set grepformat=%f:%l:%c:%m

" Now :grep uses ripgrep
:grep 'TODO' src/
:copen
Quickfix list:
src/main.py|15 col 5| # TODO: refactor this
src/utils.py|42 col 5| # TODO: add error handling

Tips

  • Add --smart-case for case-insensitive matching when pattern is lowercase
  • rg automatically skips .git/, node_modules/, and files in .gitignore
  • Use :grep! (with bang) to not jump to the first result
  • For async grep, use plugins like vim-dispatch or Neovim's job control

Next

How do I return to normal mode from absolutely any mode in Vim?