Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

grep

vim

People also ask

How do I search a whole word in Vim?

To search using Vim/vi, for the current word: In normal mode, you can search forward or backward. One can search forward in vim/vi by pressing / and then typing your search pattern/word. To search backward in vi/vim by pressing ? and then typing your search pattern/word.

How do I grep a word in Vim?

You simply type :Grep foobar , and it will search in your current directory through all file extensions (except json and pyc; you can add more to the blacklist). It also displays the results in a nice little buffer window, which you can navigate through with normal HJKL keys, and open matches in the main editor window.

How do I search for a word in vi editor?

To find a character string, type / followed by the string you want to search for, and then press Return. vi positions the cursor at the next occurrence of the string. For example, to find the string “meta,” type /meta followed by Return.

How do I search for a string in Vim?

Perform a basic search in Vim If you are in insert mode, simply press the 'ESC' key. To perform a basic search of the string or text that you want, move to the beginning of the file and simply press the forward-slash ( / ) key. Then type the search string and press ENTER on the keyboard to start searching.


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


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