views:

173

answers:

4

I want to mass-edit a ton of files that are returned in a grep. (I know, I should get better at sed).

So if I do:

grep -rnI 'xg_icon-*'

How do I pipe all of those files into vi?

+7  A: 

The easiest way is to have grep return just the filenames (-l instead of -n) that match the pattern. Run that in a subshell and feed the results to Vim.

vim $(grep -rIl 'xg_icon-*' *)
jamessan
Also note that vim doesn't like it when standard input is from a pipe, so the above is much better than 'grep -rIl ... | vim'.
Kaleb Pederson
Vim is happy with standard input when you invoke it with `vim -`
a paid nerd
@Kaleb vim works very well when it comes from a pipe. Just don't forget the call to xargs, in that case. When the pipe fills vim with contents and not filenames, the paid nerd answered your remark: don't forget the dash.
Luc Hermitte
+2  A: 

A nice general solution to this is to use xargs to convert a stdout from a process like grep to an argument list.

A la:

grep -rIl 'xg_icon-*' | xargs vi
Benj
A: 

if what you want to edit is similar across all files, then no point using vi to do it manually. (although vi can be scripted as well), hypothetically, it looks something like this, since you never mention what you want to edit

grep -rnI 'xg_icon-*' | while read FILE
do
    sed -i.bak 's/old/new/g' $FILE # (or other editing commands, eg awk... )
done
ghostdog74
+1  A: 

if you use vim and the -p option, it will open each file in a tab, and you can switch between them using gt or gT, or even the mouse if you have mouse support in the terminal

Otto
Thanks a lot! This feature is awesome!
Fedyashev Nikita