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