Vim is an advanced and popular text editor which comes preinstalled in most of the Linux distributions. It is a command-line based text editor that is mostly used in non-GUI based operating systems. It is highly configurable and comes with a lot of features. A significant point regarding Vim is that it is highly optimized for repetition.

In this article, we will explain one of its most useful features that is search and replace. With a handful of configuration, you can search anything you want at the blazing speed and with finer granularity.

Method #1 Find and Replace One Occurrence at a Time (Using the slash and dot command)

The simplest way to search and replace a word in Vim is by using the slash and dot command. Slash (/) can be used to search for a word while the dot (.) can be used for replacing that word.

Follow the below simple steps to search and replace any word in Vim editor:

  • Open the file in Vim
  • Press slash (/) key along with the search term like “/ search_term” and press Enter. It will highlight the selected word.
  • Then hit the keystroke cgn to replace the highlighted word and enter the replace_term
  • Go back to normal mode. Next, hit “n” to move to the next occurrence of the search term.
  • Then press the dot (.) if you want to replace the next occurrence with the same replace_term otherwise again press the “n” key to move to the next occurrence.

It is the quickest and best method for basic search and replaces functions as it involves only a few keystrokes and less disruption to your current working. However, for searching for a word that occurs too many times, it will become a repetitive and time-consuming task.

With Vim, there is another better way to avoid this repetitive task that is using the substitute command.

Method #2 Find and Replace using the Substitute command

The substitute command can be used to perform basic to advanced search and replace functions with a single command. The syntax for this command is:

$ :s/<search_term>/<replace_term>/option

Note that you have to enter this command in normal mode.

Where

  • S: stands for substitute
  • search_term: the word you want to search and replace
  • replace_term: the word with which you want to replace it with
  • Option: c (for confirmation), g (replace all occurrence in one line), i (for ignoring the case)

Search and Replace

To perform basic search and replace using the substitute command, use the following syntax:

:%s/<search_term>/<replace_term>/g

This command will replace all instances of search_term with the replace_term.

For instance, this is our sample text:

“Ubuntu is one of the most used Linux OS. Ubuntu includes thousands of useful programs. With the Ubuntu command-line, you can accomplish almost any kind of task”.

In order to replace each occurrence of “Ubuntu” with “Debian” in the following text, the following command would be used:

Search and Replace in a single line

To search and replace the occurrence of a word only in a single line instead of the whole file, use the following syntax:

:s/<search_term>/</replace_term/g

For instance, to replace the occurrence of Ubuntu with Debian in the above sample text, following command without the % symbol would be used:

Search and Replace with Confirmation

If you want to be asked for confirmation before replacing the search term, use “c” at the end of the search command as follows:

:s/<search_term>/<replace_term>/gc

The above command will ask for confirmation before each replacement (Enter y for yes while n for no).

Case insensitive Search and Replace

When you perform search and replace in Vim, by default it is case sensitive. You can perform a case insensitive search by adding “i” at the end of command as follows:

:s/<search_term>/<replace_term>/gi

For instance, to search for the term “Ubuntu” regardless of its case (UBUNTU, Ubuntu, ubuntu, uBuntu), the following command would be used:

Search and replace the whole word

By default the substitute command search for any match whether it is partial or full. In order to match the exact search_term and then replace it with the replace_term, enclose the search _term within the “”.

For instance, in some documents, you want to search and replace the exact word “you” by “me”. In that case, the following command would be used:

It will find the word “you” and replace it with “me”. However, it will not replace the words like  “yours”.

Search and Replace Words among particular Lines

In order to search for a word among the particular lines instead of just one line or the whole file, the following syntax can be used:

:<start_line>,<end_line>s/<search_term>/<replace_term>/g

For instance, to search and replace the occurrence of Ubuntu with Debian from lines ranging from 3 to 8 in some file, the command would be:

To search and replace the occurrence of a word from the current line to the next x number of lines, the following syntax would be used:

:s/search_term/replace_term/g x

Similarly, to search and replace the occurrence of a word from the current line to the last line: the following syntax would be used:

:.,$s/search_term/replace_term/g

CONCLUSION

In this article, we have learned the two command-line ways of searching and replacing any word in a Vim editor. The first command that was using the slash and dot is the simplest and easiest method but it turns out to be repetitive when you are searching and replacing a word that occurs too many times. The other command that is substitution command once might appear difficult and complex, but once you start practicing it, you will find it extremely useful in multiple scenarios.

About the author

Vim search and replace vim

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn.