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:grepwill call; backslash-escaped spaces are literal--vimgrep— outputsfile:line:col:matchformat that Vim can parse--smart-case— case-insensitive unless the pattern contains uppercasegrepformat— teaches Vim to parse thefile:line:col:messageoutput 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
rgwithag(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. grepprgandgrepformatcan also be set per-project via a local.vimrcwithexrc.