Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: open files of the matches on the lines given by Grep?

Tags:

grep

vim

I want to get automatically to the positions of the results in Vim after grepping, on command line. Is there such feature?

Files to open in Vim on the lines given by grep:

% grep --colour -n checkWordInFile *
SearchToUser.java:170:  public boolean checkWordInFile(String word, File file) {
SearchToUser.java~:17:  public boolean checkWordInFile(String word, File file) {
SearchToUser.java~:41:          if(checkWordInFile(word, f))
like image 964
hhh Avatar asked Apr 16 '10 13:04

hhh


1 Answers

If you pipe the output from grep into vim

% grep -n checkWordInFile * | vim -

you can put the cursor on the filename and hit gF to jump to the line in that file that's referenced by that line of grep output. ^WF will open it in a new window.

From within vim you can do the same thing with

:tabedit
:r !grep -n checkWordInFile *

which is equivalent to but less convenient than

:lgrep checkWordInFile *
:lopen

which brings up the superfantastic quickfix window so you can conveniently browse through search results.

You can alternatively get slower but in-some-ways-more-flexible results by using vim's native grep:

:lvimgrep checkWordInFile *
:lopen

This one uses vim REs and paths (eg allowing **). It can take 2-4 times longer to run (maybe more), but you get to use fancy \(\)\@<=s and birds of a feather.

like image 104
intuited Avatar answered Oct 11 '22 17:10

intuited