Showing posts with label grep. Show all posts
Showing posts with label grep. Show all posts

Tuesday, September 07, 2010

vim vs. grep : external quickfix/error file, cont'd

As an enhancement to my old trick of using grep with the VIM, I have finally come up with a solution which doesn't use the temp file:

$ alias vq="vi -c ':cgetb' -c ':cfirst! ' -"
$ grep -nr whatever . | grep -v garbage | vq


The trick is to use the new VIM 7.x command :cgetb which retrieves the error list from the current buffer. So first suck in the output of the grep into the nameless buffer, tell VIM to use the buffer as a error list and then with :cfirst! to jump to first matching line (and discard the nameless buffer).

Note in alias the space after :cfirst!: it is a secret ingredient to avoid bash interpreting the ! as the history expansion mark.

Thursday, April 29, 2010

vim vs. grep : external quickfix/error file

My usual trick of using vim with grep works well when single grep invocation is sufficient.

Yet quite often I end up piping one grep's output to another grep to refine the results. And that doesn't work with my old trick of wrapping the grep MethodName *.cpp into vim -c ":grep MethodName *.cpp" as the vim's internal grep doesn't support pipes.

Digging through the documentation I found that it is possible to accomplish and it fits well to my workflow.

Start grepping(*):

$ grep MethodName *.cpp # not fine enough...
$ grep MethodName *.cpp | grep -v rubbish # much better
    # and save to a file
$ grep -n MethodName *.cpp | grep -v rubbish > tmp1.out


N.B. -n to the grep is required as VIM needs line numbers to jump to the locations.

And now tell VIM to use the produced tmp1.out error file:

$ vim -q tmp1.out

or

$ vim -c ":cf tmp1.out"

or from inside running VIM:

:cf tmp1.out

Check the :h :cf for the official documentation.

(*) Obviously in the case it doesn't have to be grep - anything what produces similar output would do. I used once a perl script to generate VIM-compatible error file from diff output to preview potential merge conflicts.

Saturday, April 25, 2009

vim + grep = visual grep

Trick I'm using quite often and is worth mentioning:

vi -c ":grep -r something files_or_dirs"

That would start VIM and tell it to run grep right after that. Very convenient.