开发者

Open all files that match a certain pattern in Vim

开发者 https://www.devze.com 2022-12-18 21:27 出处:网络
I’m in ~/src. I can do git grep _pattern_ and get开发者_开发技巧 a list of all *.cpp or *.hpp files that match this pattern.

I’m in ~/src. I can do git grep _pattern_ and get开发者_开发技巧 a list of all *.cpp or *.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 go through my directory like git grep does, and jump me to the right files.)


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

vim `git grep --name-only <your expression>`


In bash, you could do

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

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


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号