tags:

views:

109

answers:

5

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))
+3  A: 

Have a look here and here.

Bozhidar Batsov
A: 

In this particular example:

vim SearchToUser.java +170
el.pescado
+2  A: 

You could do this:

% vim "+/checkWordInFile" $(grep -l checkWordInFile *)

This will put in the vim command line a list of all the files that match the regex. The "+/..." option will tell vim to search from the start of each file until it finds the first line that matches the regex.

Correction:

The +/... option will only search the first file for the regex. To search in every file you need this:

% vim "-c bufdo /checkWordInFile" $(grep -l checkWordInFile *)

If this is something you need to do often you could write a bash function so that you only need to specify the regex once (assuming that the regex is valid for both grep and vim).

Dave Kirby
+1 interesting, also: $ vim "-c bufdo vimgrep hello *"
HH
A: 

I highly recommend ack.vim over grep for this functionality.

http://github.com/mileszs/ack.vim

http://betterthangrep.com/

Logan Koester
A: 

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.

intuited
:lp, :lnf to browse in quickfix list. If :grep, then use :cn and :cp. Cannot see any difference btw :lgrep and grep, instead of different moving commands.
HH
:lopen for :lgrep and :copen for :grep.
HH
lgrep creates a separate list for the current tab. Otherwise you end up clobbering the same global list if you do `:grep`s from different tabs. At least I think so; I usually use `lvimgrep` or `r !grep...` and just added `:lgrep` in here for the sake of completeness. I guess `*open` is not really within the scope of the question actually, that's just how I usually roll.
intuited
err actually it's for the current window, not tab.
intuited