Vim whole word search

In perl regexp syntax, \b (b means bareword) can be used for searching a whole word. I.e. \btext\b:

Will match: text, @text, <space here>text!, etc

Won't match: textile, mytext.

I've been looking for a similar feature in vim for months, with no luck, until today. I've found out how to do that, the syntax is simply  \<text\>. Works for search mode (/,?), substitute (:s), and probably for other modes as well.

Actually that's what the #, * keys do (search for the under-the-cursor whole word), and that's how I found that out.

Goodie.

7 thoughts on “Vim whole word search

  1. Shlomi Fish

    Thanks for sharing this tip. Here are a few notes:

    1. perldoc perlre says:

    \b Match a word boundary

    \B Match except at a word boundary

    So it's possible "b" means "boundary" instead of "bareword". But who really cares.

    2. Next time you're looking for how to do something using Vim, you should go to Freenode's "#vim" channel and ask there. There are many knowledgeable people there, and they can probably help you. There's also a Vim mailing list.

    3. Perhaps someone should write a Perl Regex->Vim Regex and back cheat-sheet. I recall something like that in the Vim docs, but cannot find it now.

  2. jason

    I've spent 2 hours trying to find what was wrong with this:
    \(\b\w\+\b\).*\1

    After looking at your post, I've found that it should be this:
    \(\\).*\1

    Thanks!

  3. jason

    Hmm, it lookes like the text was garbled in my last post. The correct one looks like this:

    """ \(\\).*\1 """

    replace \b with \

Leave a Reply

Your email address will not be published. Required fields are marked *