Open In App

Searching and Replacing in Vim Editor

Last Updated : 06 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Vim is an excellent version of the legacy Vi editor for terminals. It is a complete command line-based text editor that has been the go-to choice for IT professionals for decades. Vim is the Vi-IMproved version of the original text editor which contains many modern features. In this article, we shall explore one such feature, the functionality to search and replace in a file. We will see various methods of searching and replacing based on different criteria such as letter case, regular expressions, etc.

Pre-requisites

  • A Linux system.
  • Terminal with Vim editor (It comes default in all Linux distros).
  • Basic understanding of editing in Vim editor.

Creating a File in Vim

Firstly, we will create a file that we will use throughout the article to perform searching and replacing. It is easy enough to create a file in Vim, you just have to run the Vim command and pass the filename as an argument.

vim <filename>

We will create a file named geek for this tutorial.

vim geek

This will open the Vim editor and will look like the following:

2Picture2

Vim interface

Now, to type in the content, we will type ‘i’ in the command mode(which is the default mode). This will allow us to write content into the geek file. The content we are using is as follows;

Howdy!
This is a document.
This is made for geeksforgeeks.
Keep geeking!

Once, you have written this content into the file, you need to save it. To do this, you need to switch back to command mode, which can be done by pressing the ESC button. Once, you are in command mode, type the following command and press Enter:

:wq!

This will save your work.

3Picture3

Saving work in vim

Now, we can move to searching and replacing part of the tutorial.

Searching in Vim

Now, Vim provides various methods of searching but the default approach is that it searches for the first instance of the given string, whether it be a substring of a word or a word itself, and will highlight it. The syntax of searching is:

/<search word>

First, you need to open a document in Vim and then, enter command mode. After this, vim will switch to search mode and whatever you will type after ‘/’, vim will search its first occurrence in the document.

For example, we will open a dummy file and search geek in it. To do this, we need to type the following in command mode.

/geek

Output:

Picture1

Basic search in vim

As we can see, that vim highlighted the first instance of ‘geek’ in the file. Now, we shall look on different ways of searching.

1) Using case insensitive searching

By default, vim uses case sensitive searching however, it provides a command to change this to case insensitive searching. The command is as follows

:set ignorecase

This, allows to change the search to case insensitive. To enter this command, you have to be in command mode and then type in the entire command given above.

For example, after changing this, if we now search for GeEk then, we will still get the same results as above.

/GeEk

Output:

Picture2

Case Insensitive search

Here, vim searches for the given string without accounting for the letter case.

Also, you can also turn back the case sensitive search by entering the following command in command mode.

:set ignorecase!

2) Exact word matching

Another method of searching in vim is to search the given word as a proper string and not a substring. For example, in previous examples, we searched for geek however, we got results even though it was part of the word geeksforgeeks. This is the default setting in Vim for searching however, it can be changed by wrapping the word we search for inside the given characters

\< word >\

For example, we will search for the first string instance of is in the document. In the default mode, it should highlight the ‘is’ inside the ‘this’ word of the first line but, because of exact word matching, it will highlight the second word in first line.

/\<is\>

Output:

Vim

Picture3

Exact word searching in Vim

Replacing With Searching in Vim

Now, that we have learned how to perform searches in Vim, we will learn how to replace the searched words. To replace a searched word in vim, we have the :substitute command or :s in short. The syntax is as follows:

:%s/search_word/replace_word/g

Now, let us go through each field in this syntax to understand it better:

  1. %s – The first part after ‘:’ decides the scale of replace. It can either be for the entire document (%), specific line numbers (e.g 3), or a range of lines (e.g. 3,7). The ‘s’ is for the substitute command.
  2. search_word – The next field followed by the ‘/’ is the word we want to replace, you can use the default searching, regex searching or exact word searching here.
  3. replace_word – Next, it is takes the world we want to replace the searched word.
  4. g – These are extra options; here ‘g’ means global which will replace every occurence in all specified lines instead of the first occurence. Also, there is an option ‘c’ which asks for confirmation at every search.

Now, we will look at some examples.

Example 1

We shall use the same document as before and now, we will search for the word ‘is’ and replace it with ‘was’ for all occurrences in the entire document. The command to do this will be as follows:

:%s/is/was/g

Picture4

Replacing a searched word in vim

Vim asks for confirmation once if the ‘c’ option is not given with the global option. Now, when you are prompted like this, press enter and the command will work as follows:

Picture5

Output of replacing

As we can see that all the instances of ‘is’, even those as substrings of words were changed to ‘was’ as explained in the searching section. Now, to keep the changes to your file, you need to enter the command to write and save.

:wq!

Example 2:

In this example, we shall search and replace for a specific lines instead of entire document. Here, we will change the word ‘is’ only in line 3, to ‘was’, instead of entire document without the global option, which means that only the first instance of ‘is’ in line 3 should be changed to ‘was’.

:3s/is/was

Picture6

Specific search and replace

As we can see, only the first instance of ‘is’ in line 3 is changed. Once done, you can save and exit with the command given in example 1.

Example 3:

In this example, we will show how to search and replace in a range of lines and prompt for confirmation for every instance. We shall search for the word ‘geek’ and replace it with ‘gfg’ in line from 3 to 4 while asking for every instance. The command is as follows:

:3,4s/geek/gfg/gc

Picture7

Searching in a selected lines and prompting for replace conformation

After pressing ENTER after typing in the command, you will be prompted to confirm the replacement for each occurrence of the word ‘geek’. If you want to change that occurrence press ‘Y’, if not then, press ‘N’. After you give the response, you will primpted with the confirmation for next occurrence until there are none left. Once done, you can save and exit with the command given in example 1.

Conclusion

In this article, we learned about the vim editor. We learned how searching is done in Vim, what are the default search settings and how we can change them according to our requirements. Then, we looked into replacing the searched word with a new word. We explained the :substitute command in complete detail and gave multiple examples to explain different options available with it.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads