Vim Regex - Word Boundaries

27 Dec 2012

Vim regex is different from many of other popular regex dialects out there, like Perl, Python etc. Therefore Vim beginners might find some of the regexs work perfectly in Python or Bash might not work as expected in Vim.

The first gotcha is probably not knowing there are some characters needed to be escaped in Vim regex. Having that in mind, then you can now express word boundaries (beginning and end of a word) easily with regular expression like this:

\<word\>

\< matches beginning of a word, \> matches end of a word. With such expression, you can search and replace exactly that word (but not words that contain that string) accurately:

:%s/\<oldword\>/newword/g

This is an important tip for your Vim regex lesson.