Take the 2-minute tour ×

I'm in ~/src

I can do

git grep _pattern_

and get a list of all *.cpp */hpp files that match this pattern

Now, I would like to go through all the files that match the pattern and make edits on them. How do I do this in vim? (basically I want vim to grew through my directory like git grep does, and jump me to the right files).


share|improve this question
add comment

5 Answers

up vote 10 down vote accepted

You can use the single inverted commas (also a unix shell feature), something like:

vim `git grep --name-only <your expression>`
share|improve this answer
Most people call them backticks ;) –  sirlancelot Feb 1 '10 at 18:10
Agreed, it’s really a grave misuse of terminology. –  Josh Lee Feb 11 '10 at 23:52
+1 just for 'single inverted commas'. –  Matt Parker Mar 22 '12 at 17:14
add comment

By properly using the quickfix list, you can even go immediately to the the right line (using the :help quickfix commands, eg. :cn or :cw). So, if you are using bash or zsh:

vim -q <(git grep foo)
share|improve this answer
add comment

you can use the args ex command:

:args *.cpp *.hpp

This will open all cpp and hpp files in the current directory.

You can use any file path expansions available to :grep as well.

share|improve this answer
add comment

You could possibly set the grepprg and grepformat options to run git grep... and interpret the result. This would then let you run the command :grep and read the results into the quickfix buffer - see :h quickfix for more information. You can then step through them with :cnext and :cprev, or :copen to open a separate window with the list of files - putting the cursor on a filename and pressing return will open that file for editing.

The advantage of this over Zoran's and ammoQ's suggestions is that it will not read the files into memory until you want to edit them. Their suggestion will load possibly hundreds of files into memory at once, and can be a nightmare to manage. It is also cross platform so should work on Windows without having to use a third-party shell such as cygwin bash.

share|improve this answer
add comment

In bash, you could do

vim $(grep -l _pattern_ *.cpp *.hpp)

but that's a bash feature, not a vim feature.

share|improve this answer
add comment

Your Answer


