How to Find or Search a Word in Vim or Vi Text Editor

How to Find or Search a Word in Vim or Vi Text Editor
How to Find or Search a Word in Vim or Vi Text Editor

Find or Search a Word in Vim or Vi Text Editor

Vim is a popular text editor that comes preinstalled on macOS, Linux Distros and BSD Like operating systems. Vim is a free and opensource text editor available today. Finding or searching a word is a frequent task that we often do. In this tutorial, you are going to learn How to Find or Search a Word in Vim or Vi Text Editor.

Finding a Word in Vim or Vi in Normal Mode

Follow below instructions to search a word in Vim:

  1. If you are in any other mode then press the Esc key to come in Normal Mode.
  2. Now, Vim supports search in two ways forward or backward. If you want to search forward then press / and type the word you want to search.
  3. Otherwise, If you want to search backward then press ? and type the text you want to search followed by Enter the key.
  4. Now once you get the occurrence of the word. Then you can press N to get the next occurrence of the word in the forward direction.
  5. Otherwise, you can press n to get the next occurrence of the word in a backward direction.

Searching Word in Vim or Vi Examples

The following are the examples for searching words in Vim or Vi.

Search a Word in Vim in the Forward Direction

In this example, we are going to search word systemd inside the file /etc/group.

First, open /etc/group file using the following command:

vim /etc/group

Now press Esc key to come in normal mode.

Then press / key and enter the word systemd that we want to search and hit Enter:

/systemd

"How

You can press n key to get the next occurrence in the backward direction. You can also press N key to get the next occurrence in the forward direction.

Search a Word in Vim in the Backword Direction

In this example, we are going to search word systemd inside the file /etc/group.

First, open /etc/group file using the following command:

vim /etc/group

Now press Esc key to come in normal mode.

Then press ? key and enter the word systemd that we want to search and hit Enter:

?systemd

"How

You can press n key to get the next occurrence in the backward direction. You can also press N key to get the next occurrence in the forward direction.

How to Highlight the Words When Search

You can enable highlight words when searching by using the :set hlsearch command. To enable it, inside the normal mode of Vim enter the following command.

:set hlsearch

To stop highlighting words when search, enter the following command:

:set !hlsearch

Search a Current Word in Vim or Vi

The word where the cursor is located called the current word. To find current word follow below instructions:

  1. Move the cursor on the word that you want to search.
  2. Now press * to search the word in the forward direction. You can also get the next occurrence by pressing the * key.
  3. You can press # to search the word in the backword direction. You can also get the next occurrence by pressing the # key.

Conclusion

In this tutorial, you have learned How to Find or Search a Word in Vim or Vi Text Editor. If You have any queries regarding this please comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here