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

How do I search for a pattern across all files in my project without leaving Vim?

Answer

:vimgrep /pattern/ **

Explanation

:vimgrep /pattern/ ** searches recursively through all files in the current working directory tree using Vim's own regex engine, populating the quickfix list with every match. It requires no external tools and understands the same patterns you use in / searches — making it a powerful built-in alternative to :grep.

How it works

  • :vimgrep runs Vim's internal grep across one or more files
  • /pattern/ uses Vim regex (supports \v, \c, \<word\>, etc.)
  • ** is the glob for "all files, recursively" — Vim expands this from the current directory
  • Results land in the quickfix list, navigable with :cn (next), :cp (prev), :copen (show list)

Example

Find every TODO comment across all files in your project:

:vimgrep /TODO/ **
:copen

The quickfix window shows each match with file, line, and content. Press <CR> to jump to any entry.

Filter by file type to avoid searching binaries:

:vimgrep /\<apiKey\>/ **/*.go

Tips

  • Add g flag to collect all matches per line, not just the first: :vimgrep /pat/g **
  • lvimgrep sends results to the location list (window-local) instead of the shared quickfix list — useful when you have multiple searches open simultaneously
  • After populating the quickfix list, use :cdo s/old/new/gc to interactively apply changes across every match in the project
  • For very large projects, :grep backed by ripgrep (:set grepprg=rg\ --vimgrep) is faster, but :vimgrep needs no configuration and works everywhere

Next

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