How do I search for a pattern across all files in my project with Vim's built-in grep?
Answer
:vim /pattern/ **
Explanation
:vimgrep /pattern/ ** (shortened to :vim) is Vim's built-in project-wide search. It scans every file matched by the glob, populates the quickfix list with all hits, and lets you navigate between them with :cnext and :cprev. No external tools required.
How it works
:vimis the short form of:vimgrep/pattern/— any Vim regex, the same syntax used in/search**— a recursive wildcard: matches all files in the current directory and every subdirectory- Results go into the quickfix list, openable with
:copen
Example
Search all files for TODO comments:
:vim /TODO/ **
:copen
Search only Go files for an interface name:
:vim /MyInterface/ **/*.go
After the search, navigate matches:
:cnext " jump to next match
:cprev " jump to previous match
:cfirst " go back to first match
Tips
- Add
jflag to skip jumping to the first match::vim /pattern/ **j - Use
%instead of**to search only the current file::vim /pattern/ % - Combine with
:cdoto apply changes to all matches::vim /old/ ** | cdo s/old/new/ | update - For large codebases,
:vimgrepis slow compared to:grep(which delegates togrepprg). Setgrepprg=rg\ --vimgrepto use ripgrep via:grepwhile keeping:vimgrepfor regex features