178

I'd like to be able to search

/the\b

to find "the" but not "then".

I also tried searching with very magic turned on:

/\vthe\b

4 Answers 4

196

/the\>

See :help /ordinary-atom

I assume "regexp" means PCRE. It is worth noting that Vim's regex syntax differs from (and apparently predates) PCRE.

See also:

3
  • 4
    why wouldn't \b work? vim doesn't conform to regex?
    – Kwolf
    May 10, 2017 at 16:59
  • 9
    I'm assuming you mean Perl Compatible Regular Expressions. Vim's regex syntax predates PCRE. I added links to related questions. May 12, 2017 at 4:04
  • "regexp" does not mean PCRE. It is simply a synonym to "regex".
    – henrebotha
    May 3, 2018 at 12:33
100

Use \< and \> for word start and word end, respectively.

E.g. In your specific case you would use:

/the\>/
1
  • The reason I always forget this and always have to come back to this page is because I forget that it is a BACKSLASH, because you are inserting a special character: a word boundary. I tend to type a forward slash because I am used to that as an escape sequence, then I can't figure out why it's not working. Hopefully this will help someone with the same problem.
    – felwithe
    Apr 28 at 16:55
44

If very magic is turned on, then you shouldn't escape the > character. See what's magic search. SO in your case you'd do:

/\v<the>

it would search for only the word 'the'.

0
18

if you are trying to search a word at your cursor. you can just hit *, or # for backward search.

1
  • This unfortunately ignores the sigil. (If you use it on $var, it will match "var", not "$var"). I understand why-- $ is not a word character. But looking for precise variable names is one of the most common reasons I search.
    – felwithe
    Nov 26, 2021 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.