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

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

  • :vim is 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 j flag to skip jumping to the first match: :vim /pattern/ **j
  • Use % instead of ** to search only the current file: :vim /pattern/ %
  • Combine with :cdo to apply changes to all matches: :vim /old/ ** | cdo s/old/new/ | update
  • For large codebases, :vimgrep is slow compared to :grep (which delegates to grepprg). Set grepprg=rg\ --vimgrep to use ripgrep via :grep while keeping :vimgrep for regex features

Next

How do I match a pattern only when it is preceded or followed by another pattern, without including that context in the match?