Find files with grep and open in editor

Viewed 5106

I'd like to use grep for searching through a directory and open the files with matches in an editor of choice. (emacs or vim will do.)

I can open the first match via vim $(\grep -l "static void main" *), but this won't open the other matching files. The \ in front of grep is used to use an unmodified grep, usually I have colored grep which will not work because it leads to wrong filenames.

I am aware that I can try find, pipe the results each to grep and open then the file found in question each in a new editor, with the help of xargs.


Best working solution:

USAGE:
grepe static void main or
grepv static void main.

No "" needed.

INSTALL:
Put this into your .bashrc.

#emacs:
grepe(){
    emacs $(\grep -irl "$*" .)
}
#vim:
grepv(){
    vim $(\grep -irl "$*" .)
}
4 Answers

for other editor (like sublimetext subl) I use to do that:

grep ... -l | xargs subl

the -l will only display the path so then you can directly open with your editor if it has command line support

Related