Vim, regular expressions and negative lookahead

One of Vim’s biggest strengths IMHO is it’s quick access to the search and extended support for regular expressions. It can be very frustrating at first as the syntax for the latter doesn’t follow Perl or anything else I have used (grep, etc.), but once you learn more about it, you’ll start to appreciate it.

A feature I recently discovered was negative lookahead. This can be useful in cases where you are looking for a particular string, but only if it’s not followed by another. In my case, I was looking for all references of include, but since this also matches Ruby’s include?, I wanted to exclude and not report it. The search expression I used was:

/include\(?\)\@!

There are all sorts of goodness in the Vim documentation on patterns. If you are using Vim on a daily basis, it’s worth spending a few minutes to learn what’s available. Your next search & replace could save you hours of editing or recording and getting a macro just right.