How do I do a whole-word search like grep -w
in Vim, which returns only lines where the sought-for string is a whole word and not part of a larger word?
One can use 'very-magic' \v to simplify the command:
/\v<yourword>
As mentioned in comments, \v
indicates that all alphanumeric characters have special meaning hence one needs to enter only <
and >
rather than escaping them with \<
and \>
.
\<bar\>
matches bar
but neither foobar
nor barbaz
nor foobarbaz
.
Use it like this in a substitution:
:s/\<bar\>/baz
Use it like this to list all the lines containing the whole word bar
:
:g/\<bar\>
:h pattern
is a good read.
You want /\<yourword\>
.
If your cursor is on a word, then you can press *
and it will do a word-only search for the word under the cursor.