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

How do I search with vimgrep only in specific file types?

Answer

:vimgrep /pattern/ **/*.py

Explanation

By specifying a file glob pattern with :vimgrep, you can restrict the search to specific file types. The ** pattern makes it recursive.

How it works

  • **/*.py matches all Python files recursively
  • **/*.{js,ts} matches JavaScript and TypeScript files
  • Results populate the quickfix list

Example

:vimgrep /class.*Model/ **/*.py
:copen

Searches for class definitions with "Model" in all Python files.

Tips

  • :vimgrep /pattern/g file reports every match, not just first per line
  • :vimgrep /pattern/j file does not jump to the first match
  • :grep -r --include='*.py' pattern . uses external grep (faster)
  • :set grepprg=rg\ --vimgrep makes :grep use ripgrep
  • Use :copen to see all results in the quickfix window

Next

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