r/vim•
inside_

External grep and multiple matches on the same line

Hi!

It seems external grep tools like git grep, ack or ag only report the first match from the current line.The g pattern flag for :vimgrep can report multiple matches on the same line. But :vimgrep is slow.

Here's one solution I found that could be wrapped into a function or plugin:

:vimgrep /pattern/g `git grep --name-only pattern`

Is there a better way you can think of?

Thanks!

Posted Sep 17, 2014, 10:05 AM UTC

59
miah_•

Let me introduce you to egrep, and extended regular expressions.

egrep -v '^#|^$' < httpd.conf

This will drop all empty lines and comments from a file and display to you just the things that matter.

You don't have to use egrep directly; you can just pass -E to grep itself. There are other examples in the man page.

You could also try passing multiple -e where each invocation specifies a pattern you want to match.

grep can be symlinked to: egrep, fgrep, zgrep, zegrep, zfgrep to ack as different greps, you can also just pass the right argument to grep to get the behavior.

2
inside_OP•

Pretty cool regex and commands!!

I want to navigate through every match from within vim using the quickfix window. I believe I can't do that easily with those grep commands. I'd like the behavior of vimgrep and the speed of git grep.

1
akkartikhttp://akkartik.name/about•

I don't follow. Is that egrep doing more than just saving some backslashes? I get the same behavior from:

grep -v "^#\|^$"

Perhaps the -v is interfering with my understanding of your point. Can you give an example without it?

1
miah_•

-v does a inverse match. It will display everything that does not match the regular expression passed.

Its very possible that 'grep' alone will do the same; as I mentioned egrep is basically a symlink to grep to cause it behave slightly different.

In my case; egrep may be overkill. Its just old-habits dieing hard. Growing up Solaris and realizing that things like 'grep' are not the same as 'gnu grep'.

2
dddbbbFastFold made vim fast again•

grep doesn't report the first match from the current line. It reports the matching lines. As far as I know it has no column support. However, you're right about ack. Its --column argument only works for the first column. It could probably be extended to output all matches.

Your solution looks like a good one to me.

My solution is usually to search within the quickfix (or the location it takes me to) for my query. I just want to get to the approximate location.

Why do you want this behavior? Maybe there's a better way to achieve that?

1
inside_OP•

Yes, I'd like ack to be extended, or ag, or git grep.

The quickfix navigation is awesome and I use :cn :cp all the time. The approximate location you mentionned has been working for me so far (I'm using grepprg=git grep...), but I'm looking to improve my workflow.

1
dddbbbFastFold made vim fast again•

ack is perl and ag is c so good luck, whichever is your cup of tea.

If you give it a go, you might want to file an issue first to see if it would be accepted.

1
inside_OP•

I forked the ag repo and implemented the --vimgrep option: https://github.com/inside/the_silver_searcher

Check it out if you want. I've been using my version for a day and so far it's working great. Any feedback are appreciated!

I'll make a pull request soon and hopefully it'll be accepted :-)

Regards

2
inside_OP•
2