Tip 110Customize the grep Program

Vim’s :grep command is a wrapper for the external grep program. We can customize the way that Vim delegates this task by manipulating two settings: ‘grepprg’ and ‘grepformat’. First we’ll examine the defaults, and then we’ll see how tweaking them allows us to outsource the search task to any other suitable program.

Vim’s Default grep Settings

The ‘grepprg’ setting specifies what to run in the shell when Vim’s :grep command is executed ('grepprg'). The ‘grepformat’ setting tells Vim how to parse the output returned by the :grep command (see 'grepformat'). On Unix systems, the defaults are these:

 grepprg=​"grep -n $* /dev/null"
 grepformat=​"%f:%l:%m,%f:%l%m,%f %l%m"

The $* symbol used in the ‘grepprg’ ...

Get Practical Vim, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.