Friday, September 12, 2008

Vimgrep Without Object Names

My friend Chris (Vim user extraordinaire) has contributed the following tip. It looks like it could be quite useful to any Java hackers in the crowd.

I constantly want to use :vimgrep to find usages/references to certain object types in my java code. However, when I :vimgrep for the object name, it (naturally) matches all of the 'import' statements, too. I've known for awhile that there was an idiom in the regex syntax to negate a previous atom (in this case, the 'import' at the beginning of the line) but I've never found a way to phrase it that works. Here it is:


This matches any line meeting the following two constraints:
1) contains anything (.*) followed by ObjectName
2) starts (^) with something which isn't 'import' (\(import\)\@!)

the \@! says, match if the preceding atom does not match here -- since we put ^\(import\) this means match when the first thing in the line isn't import.

There may be better ways to do this, but it's the first I've found and I'm using it like crazy now... Enjoy!

Thanks Chris!

1 comment:

Anonymous said...