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

How do I use an external grep program from within Vim?

Answer

:grep pattern files

Explanation

The :grep command runs an external grep tool and loads the results into Vim's quickfix list. It is faster than :vimgrep for large codebases.

How it works

  • :grep pattern files runs the program specified by grepprg
  • Results are parsed and loaded into the quickfix list
  • :copen opens the quickfix window to browse matches

Example

:grep -r "TODO" src/
:copen
:cnext

Tips

  • :set grepprg=rg\ --vimgrep uses ripgrep for faster searching
  • :set grepformat=%f:%l:%c:%m sets the output format parser
  • :lgrep uses the location list instead of quickfix
  • :grep! suppresses the automatic jump to the first match

Next

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