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

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:

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

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

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.

Searching and Replacing in Vim Editor

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.

Similar Reads

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

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:...

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:...

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