How To Search In Vim/Vi?

As a command-line tool searching different terms, words, sentences in a text can be done by using commands. Vim is a complex text editor and provides different search features like forwarding, backward search, whole word search, case-insensitive search, etc.

Basic Search

In the basic search, we will provide some simple terms in order to search the given term in the current file. There is two search direction named forward search and backward search. Forward search will search from the cursor’s current location to the end of the file. Backward search will search from the cursor’s current location to the start of the file. The forward search can be done with the / (forward slash) and backward search can be done with the ? (question mark).

Let’s make a forward search with the / (backward slash). We will search for the term named

  • First press ESC to change normal mode.
  • Use / and type the term, number, word you want to search. In this case, we will search for including . While typing term vim will automatically search in the background and math the first occurrence for the current term.
  • The last step is pressing Enter which will perform a search.
/including
Forward Search

Jump Next Occurence

  • By pressing the n key you can jump to the next occurence of the search.
n

Jump Previous Occurence

  • By pressing N key you can jump to the previous occurence of the search.
N

Reverse/Backward Search

The ? the question mark is used to search reverse which means from the cursor current location to the start of the file. The search term is provided after the question mark. Follow these steps for a reverse or backward search.

  • Press Enter for change into normal mode.
  • Type ? (question mark) and put the term you want to search. In this case, we will search for are.
  • The last step is pressing enter which will complete the search.
?are
Backward Search

Jump Next Occurence

  • By pressing the n key you can jump to the next occurence of the search.

Jump Previous Occurence

  • By pressing N key you can jump to the previous occurence of the search.

Search for Whole Word

By default, the Vim backward and forward search match the whole word or some part of the word. Think that when searching the are term will also match in the word software. We can search and match the complete word by putting the word inside less than sign and greater than sign. But as the less than sign and greater than sign are special signs before them the backslash will be used.

  • Press Enter for change into normal mode.
  • Type / (forward slash) and put the term you want to search with \<are\> signs. In this case, we will search for are.
  • The last step is pressing enter which will complete the search.
/\<are\>
Search for Whole Word

Search the Current Word

Vim provides a practical way to search terms. You can search current word where the cursor is located by using * (asterisk) and # (hash) signs. The asterisk sign will search forward and the hash sign will search backward.

  • Press ESC for normal mode.
  • Press * (asterisk) for forward search for the currently active word which is are. Or you can use # (hash) search backward for the currently active word which is are.
  • Press Enter.
Search the Current Word

Search History

As a practical and advanced text editor, Vim tracks the search history where every search is stored in a buffer. There are two search histories which one is forward search history and the other is backward search history. You can access the forward search history with the following steps.

  • First press ESC to change normal mode.
  • Use / and then use the up and down arrows to browse forward search history.

You can also access the backward search history with the following steps.

  • First press ESC to change normal mode.
  • Use ? and then use the up and down arrows to browse forward search history.

Search Case Sensitivity

By default, the Vim search is case sensitive which means they are is not the same Are or ARE. But there are different ways for case insensitive search temporarily or permanently. You can disable case sensitivity or enable case-insensitivity with the following command.

  • Press the ESC for normal mode.
  • Type :set ignorecase or :set ic which will work for the current session.
:set ignorecase

OR in a short form.

:set ic

You can change the search back to the case-sensitive search with the following steps.

  • Press the ESC for normal mode.
  • Type :set noignorecase or :set noic which will work for the current session.
:set noignorecase

Or in a short form.

:set noic

Search sensitivity can be also disabled and a case insensitive search can be done for a specific search by adding the \c at the end of the search term. For example, the term are can be searched in a case-insensitive manner like below.

  • Press the ESC for normal mode.
  • Type /are\c for case insensitive search.
/are\c

You can make the case insensitivity as a permanent configuration by adding the following lines into your vim configuration file ~/.vimrc .

:set ignorecase

Leave a Comment