Debug School

rakesh kumar
rakesh kumar

Posted on

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

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

Vim and its older counterpart Vi are widely used text editors. They are both open-source (free) and available on most Linux and macOS distributions.

Searching in Vim/Vi for words or patterns is a common task that allows you to navigate through large files easily.

Basic Searching in Vim / Vi
There are two ways to search for a pattern of numbers or letters in the Vim/Vi text editor.

  1. Search forward for the specified pattern

  2. Search backward for the specified pattern

The direction is determined in relation to the cursor position.

Searching Forward For Next Result of a Word
To search forward, use the command:

/pattern
Enter fullscreen mode Exit fullscreen mode

Replace pattern with the item(s) you want to find.

For example, to search for all instances of the pattern "root", you would:

  1. Press Esc to make sure Vim/Vi is in normal mode.

  2. Type the command:

/root
Enter fullscreen mode Exit fullscreen mode

The text editor highlights the first instance of the pattern after the cursor. In the image below, you can see that the result is part of a word that contains the specified pattern. This is because the command does not specify to search for whole words.

Image description

From this position, select Enter and:

jump to the next instance of the patter in the same direction with n
skip to the next instance of the pattern in the opposite direction with N
Searching Backward For a Word
To search backward type:

?pattern
Enter fullscreen mode Exit fullscreen mode

Replace pattern with the item(s) you want to find.

For example, to search for all instances of the pattern "nologin", you would:

  1. Press Esc to make sure Vim/Vi is in normal mode.

  2. Type the command:

?nologin
Enter fullscreen mode Exit fullscreen mode

Vim/Vi highlights the first instance of the specified pattern before the cursor.

Image description

Hit Enter to confirm the search. Then, you can:

move to the next instance of the pattern in the same direction with n
jump to the next instance of the pattern in the opposite direction with N
Searching for Current Word
The current word is the word where the cursor is located.

Instead of specifying the pattern and prompting Vim/Vi to find it, move the cursor to a word, and instruct it to find the next instance of that word.

  1. First, make sure you are in normal mode by pressing Esc.

  2. Then, move the cursor to the wanted word.

  3. From here, you can:

Search for the next instance of the word with *
Search for the previous instance of the word with #
Each time you press * or # the cursor moves to the next/previous instance.

Searching for Whole Words
To search for whole words, use the command:

/\<word\>
Enter fullscreen mode Exit fullscreen mode

The text editor only highlights the first whole word precisely as specified in the command.

Image description

Open a File at a Specific Word
Vim (and Vi) can open files at a specified word allowing you to skip a step and go directly to the searched term.

To open a file at a specific word, use the command:

vim +/word [file_name]
Enter fullscreen mode Exit fullscreen mode

or

vi +/word [file_name]
Enter fullscreen mode Exit fullscreen mode

For example, to open the /etc/passwd file where it first uses the term "root", use the command:

vim +/root /etc/passwd
Enter fullscreen mode Exit fullscreen mode

The text editor opens the file and the first line it displays is the one containing the term "root", as in the image below.

Image description

Case Insensitive Search
By default Vi(m) is case sensitive when searching within the file. For example, if you search for the term /user it doesn't show results with the capitalized U (i.e., User).

There are several ways to make Vim/Vi case insensitive.

To search for a specified word with the case insensitive attribute, run the command:

/\<word\>\c
Enter fullscreen mode Exit fullscreen mode

The \c attribute instructs Vi(m) to ignore case and display all results.

An alternative is to set Vim to ignore case during the entire session for all searches. To do so, run the command:

:set ignorecase
Enter fullscreen mode Exit fullscreen mode

Lastly, the configuration file can be modified for the text editor to ignore case permanently. Open the ~/.vimrc file and add the line :set ignorecase.
Highlight Search Results
Vi(m) has a useful feature that highlights search results in the file. To enable highlighting, type the following command in the text editor:

:set hlsearch
Enter fullscreen mode Exit fullscreen mode

To disable this feature run:

:set !hlsearch
Enter fullscreen mode Exit fullscreen mode

Top comments (0)