Linux logo

A little about Vim and how to search in it

Vim is only a text editor. That’s it. If you are used to using Notepad (Windows), Sublime Text (Windows / Mac), Atom (Windows / Mac), Nano (Linux), or some other text editor, Vim is just another application that permits you to compose and edit text.

Compared to other text editors, 2 facets make Vim stand out:

Efficiency

This is the big one. Vim is all about efficiency. And there are a few angles from which it approaches efficiency.

Ubiquity

This can be a fairly cool element of Vim, which is that it is everywhere. It is available on basically every significant platform you can consider. Whether you are using a Mac, Windows, or any Linux distribution, Vim’s got you covered. Specifically, if your day to day work involves working in the ssh’ed terminal sessions, Vim is the sole text editor available.

So, how we can search in Vim text editor – that is the topic of our tutorial.

How to search in Vim/vi editor

A few points to note regarding searching in Vim/vi:

  • In normal mode, you can search forward or backward.
  • You can search forward in vim/vi by pressing / and then typing your search pattern/word.
  • Now as soon as you find the occurrence of this word. Then you can press N to find the next occurrence of this word in the forward direction.
  • Otherwise, If you would like to search backward then press ? And type the text you wish to search followed by entering the key.
  • Once a word found in vim, you can press the n key to go right to the next occurrence of this word in backward.
An example of searching in vim

In this example, we will search a Word in Vim from the forward direction

In this case, we’re going to search the word “term” within the document /etc/searchtxt.

First, open /etc/searchtxt file with the following command:

vim /etc/searchtxt

Now press the Esc key to come in the normal manner.

Then press / key and input the word term which we want to search and hit Enter:

/term

It should highlight all occurrences of “term”.

Now you may press N crucial to find the next occurrence in the forward direction. If you want to go backward, press ‘n’ key/

How to Look for the current word

The term where the cursor is located known as the current word. To find current word follow below steps:

Move the cursor on the word which you would like to search.

Now press * to search the term in the forward direction. You could even get another occurrence by pressing on the * key.

You may press # to search the term in the backward direction.

Case insensitive search in Vim

We can also conduct case insensitive search in Vim with the following two config choices. For Instance, ignore case in search patterns, type:

:set ignorecase

Now search for words in different cases.

Another option is to override the ‘ignorecase’ option if the search pattern contains upper case characters.

:set smartcase "
Search a Word in Vim at the backward example

In this case, we’re going to search word “term” within the document /etc/searchtxt.

First, open /etc/searchtxt file with the following command:

vim /etc/searchtxt

Now press Esc key to come in normal mode.

Then press ? Key and enter the word “term” that we would like to search and press Enter:

?term

You can press n essential to find the next occurrence in the backward direction.

How to Highlight the Words When Search

You can enable highlight words when searching by using the following command.

:set hlsearch

To Quit highlighting phrases when search, enter the following command:

:set !hlsearch
How do I search only for “word”

A use case is how you will search phrase in vim with /word. How can I search just for test, excluding searches for test1 and test2?

Use this command

/\<test\>

If You’re working in Ubuntu, follow the steps:

Press / and type “test” to search

To search ahead press ‘SHIFT‘ key with * key

To search in backward press ‘SHIFT‘ key with # key

<h5>Command to search and replace

You may search the whole file and replace the term by using this command:

:%s/search_term/replaceterm/

In order to confirm before replacement, use this command

:%s/search_term/replace_term/c