How to search or find words in Vim or Vi Text Editor

How to search or find words in Vim or Vi Text Editor

Find or search for words in Vim or Vi text editor

Vim is a popular text editor that is pre-installed on macOS, Linux Distros, and BSD Like operating systems. Vim is a free and open source text editor currently available. Searching for or searching for words is a task we frequently do. In this tutorial, you will learn how to search or search for words in Vim or Vi Text Editor.

Search for words with Vim or Vi in normal mode

Follow the instructions below to search for words in Vim.

  1. If you are in another mode, Esc Key to start in normal mode.
  2. Currently Vim supports searching in two ways: forward or backward. If you want to search forward, press / Enter the word to search for.
  3. Otherwise, press if you want to search backwards ? Enter the text you want to search for, then continue Enter Key.
  4. Now, get the occurrence of the word. Then you can press N Get the next word forward.
  5. Otherwise, you can press n Gets the next occurrence of a word in the reverse direction.

Search for words in Vim or Vi example

The following is an example of searching for a word with Vim or Vi.

Find words forward in Vim

This example searches for a word systemd In file /etc/group.

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

vim /etc/group

Press now Esc Key to start in normal mode.

next / Press the key and enter the word systemd I want to search and hit Enter:

/systemd

How to search or find words in Vim or Vi Text Editor

Can be pressed n Key to get next occurrence in reverse direction. You can also press N A key that gets the next occurrence in the forward direction.

Search for Vim words in backward direction

This example searches for a word systemd In file /etc/group.

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

vim /etc/group

Press now Esc Key to start in normal mode.

next ? Press the key and enter the word systemd I want to search and hit Enter:

?systemd

How to search or find words in Vim or Vi Text Editor

Can be pressed n Key to get next occurrence in reverse direction. You can also press N A key that gets the next occurrence in the forward direction.

How to highlight words when searching

Use to highlight words when searching. :set hlsearch command. To enable it, enter the following command within Vim normal mode:

:set hlsearch

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

:set !hlsearch

Find current word in Vim or Vi

The word under the cursor is called the current word. To find the current word, follow these steps:

  1. Move the cursor to the word you want to find.
  2. Press now * Search forward for a word. You can also press to get the next occurrence * Key.
  3. Can be pressed # Search for words in backward direction. You can also press to get the next occurrence # Key.

Conclusion

In this tutorial, you learned how to search or search for words in Vim or the Vi Text Editor. If you have any questions about this, please comment below.

Sidebar