'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 1:[1]
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.
Solution 2:[2]
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 \>.
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andy Lester |
| Solution 2 | Jean Paul |
| Solution 3 | user1500049 |
