How To Search To Find A Word In Vim Or Vi Text Editor – phoenixNAP

Vim search

Introduction

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

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

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

Basic

search in Vim/Vi

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

.

1. Find the specified pattern

2. Find back

the specified pattern

The direction is determined relative to the cursor position

. Search forward for the

next result of a word

To search forward, use the command:

/pattern

Replace the pattern with the items you want to find

.

For example, to find all instances of the “root” pattern, you should:

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 the search for full words.

From this position, select Enter and

: jump to the next instance of the pattern in the same direction with

  • njump to the next instance of
  • the pattern in the

  • opposite direction with N

Find back for

a word To search

backwards, type:

?pattern

Replace the pattern with the items you want to find.

For example, to find all instances of the “Nologin” pattern, 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

.

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

Go 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

Current word search

The current word is the word where the cursor is located.

Instead of specifying the pattern and asking Vim/Vi to find it, Move the cursor to a word and instruct it to look for 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 desired word.

3. From here, you can

: Find the next instance of the word with *Find the previous instance of the word with #

Every time you press * or # the cursor moves to the next/previous instance.

Full word search

To search for whole words, use

the command: /<

  • word> The text editor only highlights the entire first word

accurately as specified in the command.

Open a file in a specific word

Vim (and Vi) can open files in a specific word, allowing you to skip a step and go directly to the searched term.

To open a file

in a specific

word, use the command: vim +/word [file_name]

or

vi +/word [file_name]

For example, to open the /etc/passwd file where you first use 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

.

Search that is not case-sensitive

By default, Vi(m) is case-sensitive when searching within the file. For example, if you search for the term /user, it does not display results with an uppercase U (that is, 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 configure Vim to ignore case case for the entire session for all searches. To do this, run the command::

set ignorecase

  • Finally, the configuration file can be modified so that the text editor ignores case permanently. Open the ~/.vimrc file and add the :set ignorecase line.

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

Search History

Vi(m) keeps track of the search commands used in the session. Navigate through the commands used above by typing ? or/y 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.

Contact US