tags:

views:

217

answers:

4

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).

Thanks!

+2  A: 

In bash, you could do

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

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

ammoQ
+2  A: 

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

vim `git grep --name-only <your expression>`
Zoran Regvart
Most people call them backticks ;)
sirlancelot
Agreed, it’s really a grave misuse of terminology.
jleedev
+2  A: 

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.

Dave Kirby
A: 

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.

zerotao