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

How do I search for a pattern across multiple files?

Answer

:vimgrep /pattern/ **/*.ext

Explanation

The :vimgrep command searches for a pattern across multiple files and populates the quickfix list with the results. It uses Vim's internal regex engine.

How it works

  • :vimgrep /pattern/ files searches the given files
  • **/*.ext is a glob for all matching files recursively
  • Results go into the quickfix list
  • :copen opens the quickfix window to browse results

Example

:vimgrep /TODO/ **/*.py
:copen

This finds all TODO comments in Python files and opens the quickfix list.

Tips

  • :cnext/:cprev navigate between matches
  • :vimgrep /pattern/g file lists every match (not just one per line)
  • :grep uses the external grep command instead (faster for large projects)
  • :lvimgrep uses the location list instead of quickfix

Next

How do you yank a single word into a named register?