:grep and quickfix window

67 views
Skip to first unread message

Greg Klein

unread,
Sep 15, 2008, 5:51:42 PM9/15/08
to vim...@googlegroups.com

I would like to use an external grep for my vim grep searches. So I have this in my _vimrc:

se grepprg=c:/cygwin/bin/grep.exe

 

Now I do

:grep XYZ *.c

 

Now I would like to look at the result of the grep in a quickfix window. But if I do

:cwindow

 

a quckfix window is not opened. If I instead do :copen, then I do see the grep result. But each line in the result is prepended with ‘||’ like this:

 

|| foo.c:line that contains XYZ

|| bar.c:and another line that contains XYX

 

I can’t seem to open the files by selecting them from the :copen window (put cursor on them and type <CR>). Are the ||’s the problem? Does it mess up grepformat?

 

What causes the ||’s to appear? If I run c:/cygwin/bin/grep.exe outside of vim I do not get them. If I do not set grepprg then the default for grepprg is findstr.exe. In that case, I do not get the ||’s. If I set grepprg to findstr.exe, then I do get the ||’s.

 

Greg

Erik Falor

unread,
Sep 15, 2008, 6:01:44 PM9/15/08
to vim...@googlegroups.com
The pipes separate filename from line number & column from the matched string.  Here's a picture:

Starter.cs|3| // Version $Id: Starter.cs,v 1.80.2.23 2008/09/04 19:31:51 rmehta Exp $
Starter.cs|49| /// Starter is the main launching class.
Starter.cs|51| sealed public class Starter
Starter.cs|66| private Starter()

To get my output to look like this, I have to run grep with the following args:
grepprg=grep -HnE

The -H forces grep to always print the filename containing the match, and -n tells grep to print the line number of the matching line.

Now, I have to tell vim how to parse the output generated by this invocation of grep:
grepformat=%f:%l:%m

 

Greg






--
Erik Falor
Registered Linux User #445632 http://counter.li.org

Erik Falor

unread,
Sep 15, 2008, 6:08:00 PM9/15/08
to vim...@googlegroups.com
I forgot to mention that because the output of the search you're running doesn't include a filename nor a line number, Vim is unable to jump your cursor to the location of the matched string.  As soon as you change your grepprg to output that info, and your grepformat to parse it, the quickfix list will be much more helpful.

In principle, this all works in the same way as Vim's compiler integration; it just parses the compiler's output looking for things that look like error messages with file locations it can jump to.

I should also mention where all of this stuff is documented:

:help grep - then scroll down ~62 lines to read about "External grep"
:help quickfix
:help errorformat
 

 

Greg






--
Erik Falor
Registered Linux User #445632 http://counter.li.org

Gary Johnson

unread,
Sep 15, 2008, 6:41:26 PM9/15/08
to vim...@googlegroups.com

The problem is that you forgot to include the -n argument as part of
grepprg. That is, you should set greprg like this:

:set grepprg=c:/cygwin/bin/grep.exe\ -n

See

:help grepprg

Regards,
Gary

Reply all
Reply to author
Forward
0 new messages