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 current location to the end of the file. Backward search will search from the cursor 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).

Lets make 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.
Forward 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.

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

Jump Next Occurence

  • By pressing the n key you can jump to the next occurence of the search.
READ  Linux SCP Command Tutorial - Transfer Files Securely

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 then sign and greater then sign. But as the less then sign and greater then 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.
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 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 advenced text editor, Vim tracks the search history where every search is stored in a buffer. There are two search history which one is forward search history and 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.
READ  Linux kill Command Tutorial

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 the are is not the same Are or ARE. But there are different ways for case insesitive search temporaryly or permanently. You can disable casesensitivity 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.

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.

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.

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