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:
Note that you have to enter this command in normal mode.
- 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:
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:
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:
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:
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:
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:
Similarly, to search and replace the occurrence of a word from the current line to the last line: the following syntax would be used:
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.