Mar 4, 2022 3 min read

How to Search in Vim / Vi

Learn How to Search in Vim with our step-by-step guidance. This tutorial explains how to use Vim / Vi to execute search operations.

Search in Vim/Vi
Table of Contents

Introduction

Before we begin talking about how to Search in Vim / Vi, let's briefly understand – What is Vim?

On macOS and most Linux variants, Vim or its predecessor Vi comes preinstalled. When working with files, one of the most common chores is searching for text. When your preferred editor is unavailable, knowing the basics of Vim may come in handy.

This tutorial explains how to use Vim / Vi to execute search operations. We will also address a few FAQs on how to search in Vim/Vi.

You must be in normal mode to search in Vim. You're in this mode when you open the Vim editor. Simply use the Esc key to return to regular mode from any other mode.

Using the / (forward slash) and ? (question mark) commands in Vim, you can quickly find text.

To search ahead, press /, and to search backward, press ? , to start the search, write the search pattern, and hit Enter:

It's worth noting that the search command searches for the pattern as a string, not as a single word. If you search for "gnu," for example, the search finds matches where "gnu" is incorporated in larger terms like "cygnus" or "magnum."

To search for the next occurrence, press n, or to search in the opposite direction, press uppercase N.

The following are the basic stages for conducting a search in Vim:

  1. Press the / key.
  2. Type the search pattern into the box.
  3. To start the search, press Enter.
  4. To discover the next occurrence, press n, and to find the prior occurrence, press N.

Search for Whole Word

Start a search by hitting / or ?, then type \< to mark the beginning of a word, enter the search pattern, type \> to mark the end of a word, then hit Enter to complete the search.

For example, to find "gnu," you might type /\<gnu\>:

Search the Current Word

You may also find an entire word by moving the cursor over it and hitting * (asterisk) to search forwards or # (hash) to search backward. Press * or # once more to locate the next match.

Search History

Vim maintains track of all the searches you've done so far in this session. To find a prior search operation, hit / or ? and use the arrow up/down keys to navigate through the search history. Simply press Enter to start the search. Before conducting the action, you can also change the search pattern.

Case sensitivity

The search result is case-sensitive by default, searching for "GNU" will not return "Gnu."

In the Vim command line, type :set ignorecase or :set ic to ignore case. By including the command in your /.vimrc file, you can make the ignore case a default option.

Type :set noignorecase or :set noic to return to case match mode.

Another technique to cause the case to be ignored is to add a \c at the end of the search pattern. For example, /Linux\c conduct a case-insensitive search. After the pattern, an uppercase \C triggers a case match search.

FAQs on How to Search in Vim/Vi

Can I search for text in a case-insensitive manner in Vim/Vi? 

Yes, add the set ignorecase command in your Vim configuration to make searches case-insensitive.

How can I search for the next occurrence of the pattern in Vim/Vi? 

Press n in command mode to search forward for the next occurrence of the pattern.

Is it possible to search and replace text in Vim/Vi? 

Yes, you can use the :s/pattern/replacement command in command mode to search for a pattern and replace it with the specified replacement text.

How can I search only within a specific range of lines in Vim/Vi? 

In command mode, use :X,Y/pattern to search for the pattern within the lines from X to Y.

How can I replace all occurrences of a pattern in Vim/Vi? 

To replace all occurrences of a pattern, use the :%s/pattern/replacement/g command in command mode.

How do I jump to the next search result without highlighting in Vim/Vi? 

Press n in command mode to jump to the next search result without highlighting it.

Conclusion

In Vim/Vi, type / or ?, then input the search pattern and press Enter.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.