Seb Jones
Seb Jones

Reputation: 13

Vim: Custom grep finds files but does not open them

I have my grep program defined as follows in my vimrc:

set grepprg=grep\ -R\ --exclude=*~\ --exclude=*.swp\ --exclude-dir=vendor\ --exclude-dir=node_modules\ --exclude=tags\ --exclude=tags.vendor

When I do a search, it brings up a list of results just fine, but when I do :copen, although it will list the files, I am unable to open them.

Upvotes: 1

Views: 116

Answers (1)

mhinz
mhinz

Reputation: 3351

The quickfix/location list is filled by :grep according to the comma-separated formats specified in 'grepformat'.

'grepformat' defaults to %f:%l:%m,%f:%l%m,%f %l%m. Here, %f is the filename, %l the line, and %m the actual matched line. Every line of grep output is matched against each format until one succeeds.

If no format matches, the line will be added to the quickfix list as-is, as in your case. It will be just text and Vim doesn't know how to handle that line.

By default, my version of BSD grep returns lines like file: message for grep -R. So, taking the first format of %f:%l:%m, the line number is missing. Looking at the manpage, that's what the -n flag is for.

So, try this instead:

set grepprg=grep\ -nR\ --exclude=*~\ --exclude=*.swp\ --exclude-dir=vendor\ --exclude-dir=node_modules\ --exclude=tags\ --exclude=tags.vendor

Afterwards, :silent grep foo . | copen should work as expected!

(Psssh, the -n flag is also mentioned in :h 'grepprg'. :)

Upvotes: 2

Related Questions