67

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?

grep -w : Select only those lines containing matches that form whole words.

Can this be done in Vim?

Randall
  • 2,859
  • 1
  • 21
  • 24

4 Answers4

81
\<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.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • For matching `bar` with `foobar` and `barbaz`, one could use `g*`. From the documentation: g* Like "*", but don't put "\<" and "\>" around the word. This makes the search also find matches that are not a whole word. {not in Vi} – Ambareesh Sep 23 '20 at 18:40
  • why do some characters have special behaviour when prefixed with `\​`, and some have special behaviour when they aren't prefixed with `\​`? – mekb Apr 26 '22 at 09:12
  • 2
    @mekb, it has to do with how regular expression work and the range of available characters in ASCII. A pattern can be as basic as "match the sting `abc`" or as complex as, well… whatever you can think of. Since the syntax had to be constrained to only use printable ASCII characters, and all of them could end up in the original text, and all of them could be searched for, a mechanism was needed for separating characters from metacharacters. `<` on its own is a literal `<` character that can be matched but, prepended with a `\\`, it becomes a metacharacter that means "beginning of a word". – romainl Apr 26 '22 at 09:29
32

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.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
17

One can use 'very-magic' \v to simplify the command:

/\v<yourword>

As mentioned in comments, \v indicates that all non-alphanumeric characters have special meaning hence one needs to enter only < and > rather than escaping them with \< and \>.

Jean Paul
  • 1,439
  • 18
  • 21
rnso
  • 23,686
  • 25
  • 112
  • 234
  • 1
    \v: "make every following character except a-zA-Z0-9 and '_' have special meaning" according to the vim wiki. This has nothing to do with whole-word-search_. – Timo Jul 25 '17 at 12:04
  • 1
    @Timo, but this answer has everything to do with it. Granted, it would probably be better as a comment. – tim-phillips Nov 30 '17 at 18:41
  • 1
    I agree it is a small point and can go as a comment, but general users are much more likely to read answers than comments and I find `\v` to be a very useful method to keep commands simple and clear. – rnso Dec 02 '17 at 03:11
1
map w /\v<><Left>

This mapping is using magic with the addition of moving cursor position between the "<" and ">" pair. As soon as press 'w', you can type your word right away, and enter to perform a wholeword search.

Of course instead of 'w' you can pick your favorite letter for mapping.

user1500049
  • 993
  • 7
  • 15
  • might be better to use `\<` `\>` – mekb Apr 26 '22 at 09:13
  • I've linked to this answer from [another question](https://stackoverflow.com/a/76184738/584940) and added some additional info and caveats on this as well. See: [Searching whole word in vim - is there another way than using \<\>](https://stackoverflow.com/a/76184738/584940) – Randall May 09 '23 at 15:40