Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

The Vim editor is one of the most advanced text editors in Unix-based systems. It was developed by Bram Moolenaar and supports almost all file types. It’s sometimes referred to as the programmer’s editor.

In this tutorial, we’ll explore the different methods available for performing a search and replace with the Vim editor.

2. Setup

The Vim editor comes pre-installed in some Linux distros.

Let’s first find out if Vim editor is installed:

$ vim --version
Command 'vim' not found, but can be installed with:
sudo apt install vim         # version 2:8.2.2434-3ubuntu3.2
sudo apt install vim-tiny    # version 2:8.2.2434-3ubuntu3.2
sudo apt install vim-athena  # version 2:8.2.2434-3ubuntu3.2
sudo apt install vim-gtk3    # version 2:8.2.2434-3ubuntu3.2
sudo apt install vim-nox     # version 2:8.2.2434-3ubuntu3.2
sudo apt install neovim      # version 0.4.4-1

We can install Vim editor on a Debian-based distro with this command:

$ sudo apt install vim

On Arch-based distros, we can use this command:

$ sudo pacman -S vim

We’ll need a sample text file that contains data that we’ll search and replace. So let’s create a file called sample_text.txt:

$ touch sample_text.txt

Next, we open it with the Nano editor:

$ nano sample.txt

Finally, we can paste in these lines and save the changes:

This is an article covering search & replace with Vim.
Vim is a powerful text editor used on the CLI.
We'll cover different scenarios of search & replace in this article.
This will be the last line on the sample file.

3. Basic Search and Replace Using Slash and Dot

The simplest way to perform a search and replace in Vim editor is using the slash and dot method. We can use the slash to search for a word, and then use the dot to replace it.

Let’s open the sample.txt file using the Vim editor:

$ vim sample.txt

Inside Vim editor, we need to press the forward-slash(/) key, then search for the word “article”:

/article

This will highlight the first occurrence of the word “article”, and we can press the Enter key to jump to it.

We can then type in the cgn command combo:

cgn

This Vim editor command finds the last thing we searched for, delete it, and then put us into insert mode.

While in insert mode, let’s type in the word “tutorial”. This will replace the first occurrence of the word “article” with “tutorial”. 

We can then press the Esc key to return to normal mode.

Next, we need to press the “N” key to jump to the next occurrence of the word “article” and press the Dot (.) key to auto-replace it with the word “tutorial”.

Our file should now match this after making the changes:

This is an tutorial covering search & replace with Vim.
Vim is a powerful text editor used on the CLI.
We'll cover different scenarios of search & replace in this tutorial.
This will be the last line on the sample file.

This is the simplest method to perform basic search and replace in Vim editor. However, searching for a word occurring multiple times would become a repetitive and tedious task.

4. Search and Replace Using the substitute Command

We can also use the Vim editor substitute command to perform basic and advanced search and replace functions.

The substitute command has this basic syntax:

:s/<search_phrase>/<replace_phrase>/options

We need to enter this command in normal mode.

The “:s” represents substitute and search_phrase is the word we want to search for. replace_phase represents the word we want to replace the search word with. The options include c for confirmation, i for ignoring the case, and to replace all occurrences.

4.1. In a Single Line

Let’s search and replace all occurrences on a single line:

:s/article/tutorial/g

This replaces the first occurrence of the word “article” with “tutorial”. The sample.txt file should now resemble this:

This is an tutorial covering search & replace with Vim.
Vim is a powerful text editor used on the CLI.
We'll cover different scenarios of search & replace in this article.
This will be the last line on the sample file.

4.2. All Occurrences

To search and replace all occurrences on every line of the file, we need to slightly modify the previous command:

:%s/article/tutorial/g

The % symbol lets us access all the content in the file, and we can replace all occurrences in each line.

The sample.txt file should match this after replacing all occurrences:

This is an tutorial covering search & replace with Vim. 
Vim is a powerful text editor used on the CLI. 
We'll cover different scenarios of search & replace in this tutorial. 
This will be the last line on the sample file.

4.3. Case-Insensitive

By default, the substitute command is case-sensitive whenever we use it to perform a search and replace.

We can perform case-insensitive searches by adding the “i” option at the end of our previous command.

Let’s replace all occurrences of the word “vim” in the sample.txt file:

:%s/vim/baeldung/gi

Once we’ve made the changes, the lines in the sample.txt file should match this:

This is an article covering search & replace with baeldung.
baeldung is a powerful text editor used on the CLI.
We'll cover different scenarios of search & replace in this article.
This will be the last line on the sample file

4.4. With Confirmation

The substitute command also comes with the confirmation “c” option. Adding this option at the end of the search and replace command gives us a prompt before a replacement is made.

Let’s replace all occurrences of the word “article” with confirmation:

:%s/article/tutorial/gc

Each time we get a prompt, we have the option to press “y” for yes, “n” for no, and “a” for all occurrences.

We can also press “q” to quit or “l” to replace only one line.

4.5. Within Specific Lines

The substitute command allows us to search and replace within specific lines in the file instead of searching through only one line or the whole file.

It has this basic syntax:

:start_line_number, end_line_number s/<search_term>/<replace_term>/g

Let’s replace the occurrence of the word  “Vim” on the first line only:

:0, 1 s/vim/baeldung/gi

We can also specify the number of lines to search through from the current line.

Assuming we were on the second line, let’s replace the word “article” on the third line with “tutorial”:

:s/article/tutorial/g 2

Since our sample.txt file only contains four lines, we can’t specify a number that’s outside the range of lines present.

Finally, in cases where we have multiple lines, we can search from the current line to the last line.

Let’s replace the word “article” with “tutorial” from the third line in the sample.txt file:

:.,$s/article/tutorial/g

Here, we’re specifying a range where the dot (.) represents the current line and “$” represents the last line.

4.6. Whole Word

The substitute command finds both partial and full matches by default. This allows us to only change some parts of a word instead of the whole word. However, in some special cases, we might need to search for an exact word, then replace it.

Let’s match the exact word “cover” and then replace it with “go through“:

:s/\<cover\>/go through/gi

Our sample.txt file should match this after executing this command:

This is an article covering search & replace with Vim.
Vim is a powerful text editor used on the CLI. 
We'll go through different scenarios of search & replace in this article.
This will be the last line on the sample file.

We can notice that it only replaced the word “cover” in the third line and skipped the word “covering” in the first line.

5. Conclusion

In this article, we’ve explored the different methods we can use to perform a search and replace in Vim editor.

The first method is the easiest for quick edits. However, it’s limited and tedious for multiple edits.

The second method involves using the Vim editor substitute command, which offers us more flexibility and is more suited for complex searches and replacements.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!