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

July 31, 2020

Introduction

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.

This tutorial shows you how to search in Vim/Vi with practical examples.

How to search in Vim/Vi

Important: Make sure you are in Normal Mode before you start searching. This allows in-text navigating while using normal editor commands. Vi and Vim open a file in normal mode by default.
To switch to normal mode, press Esc.

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

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

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.

Search forward for a pattern in Vim.

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

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

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

Search in Vi/Vim backward.

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

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

screenshot of searching and finding a whole word in vim

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]

or

vi +/word [file_name]

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

vim +/root /etc/passwd

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

example of opening a file in vim at a specific word

Note: An alternative to opening a file at a specific word is opening a specified line number. To enable line numbering, refer to How to Show or Hide Numbers in Vi/Vim.

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

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
  • 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

To disable this feature run:

:set !hlsearch

Note: Vim color schemes are another useful feature for practical syntax highlighting. To learn more, check out How To Change And Use Vim Color Schemes.

Search History

Vi(m) keeps track of search commands used in the session. Browse through previously used commands by typing ? or / and using the up and down keys.

Conclusion

After reading this article, you have multiple options to search and find words using Vim.

Once you find the searched term, you can move on to cutting, copying, or pasting text.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Save a File in Vi / Vim & Exit
September 22, 2022

Vim (Vi IMproved) is a well-known, open-source text editor for Linux or Unix systems. It is a powerful and...
Read more
How to Delete Line in Vim on Linux
September 26, 2024

Vim allows you to delete entire lines, words or characters using various Vim commands. Learn the most...
Read more
How to Undo and Redo Changes in Vim / Vi
April 28, 2020

Mastering basic Vim commands includes learning how to undo and redo changes in this text editor. Knowing how...
Read more
How to Exit (Quit) Linux Vim/Vi editor
June 12, 2024

Vim text editor is one of the most widely used text editor. For that reason, you should learn some of the...
Read more