Search code examples
vimgrepstring-matching

How to do whole-word search similar to "grep -w" in Vim


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?


Solution

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