Using grep in vim

neerajsharma
Vim Tips
Published in
1 min readOct 7, 2013

To set the grep correctly in vim use something like following in your .vimrc

set grepprg=c:\tools\git\bin\grep.exe\ -nir\ $*

Now you run the grep inside vim like

:grep MySearch *.cpp

This will show the first match and use the quickfix window

Then you can do following

:copen to show the list

:cn and cp to go previous and next

:ccl to close the quickfix window

You can also use

:grepa MySecondSearch *.*

This will add to the existing quickfix list instead of opening a new one

:lgrep MySearch

The above will use the location list instead of the quickfix window.

NOTE: The difference between location list and quickfix is there is just one global quickfix list but there are separate locationlist per vim window.

All commands are same except you use

:lopen and :lp and :lne and :lcl for the same commands

Grep uses grepformat to jump to files so if you are using some other program you can always set the format accordingly so vim understands its results and can jump to file using the same

:set grepprg=lid\ -Rgrep\ -s
:set grepformat=%f:%l:%m

Another concept is going back and forth between multiple searches

You do

:grep Search1 *.*

then navigate using copen cn cp etc

then do

:grep Search2 *.*

then navigate using copen etc.

then you can do

:colder to go to quickfix for Search1

and

:cnewer to go to quickfix for Search2

(You can do that for multiple searches basically)

Happy vimming and consider donating if you use this wonderful editor.

--

--