181

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
joeljpa
  • 317
  • 2
  • 13
kortina
  • 5,821
  • 4
  • 24
  • 28

4 Answers4

200

/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:

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Adam Monsen
  • 9,054
  • 6
  • 53
  • 82
103

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

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

/the\>/
einpoklum
  • 118,144
  • 57
  • 340
  • 684
Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
  • 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 '23 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'.

18

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

Rocky
  • 5,486
  • 8
  • 32
  • 36
  • 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 '21 at 18:25