How to Search and Replace Text in Vim Editor

The term search and replace might be a simple phrase to users not inclined to use non-GUI OS environments like the command line.

To a Linux user like one dedicated to programming and software development projects, you need a performant text editor like vim to handle the editing of your script and code files.

Vim text editor might appear non-user-friendly to beginners, but you will blend in like a natural color once you get used to it.

This article seeks to address the issue of search and replace under the Vim text editor in Linux.

Install Vim Text Editor in Linux

It is not a guarantee that the Vim editor is automatically installed on your Linux system. Therefore, refer to the following installation commands to make sure it’s installed on your Linux OS distribution.

$ sudo apt install vim            [On Debian, Ubuntu and Mint]
$ sudo yum install vim            [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a app-editors/vim  [On Gentoo Linux]
$ sudo pacman -S vim              [On Arch Linux]
$ sudo zypper install vim         [On OpenSUSE]    

Search and Replace Text in Vim Editor

Consider the following text file opened in vim text editor.

$ vim vim_demo.txt 
Open File in Vim Editor
Open File in Vim Editor

We will assume that we are dealing with a large file and want to search and replace some text in it.

Press the [Esc] key to switch to command mode. The command :s or substitute command is used to search and replace texts in vim.

Consider the following substitute command:

:s/03/unknown/g 

Supposing the current line is i am line 03, 03 will be replaced with unknown.

Search and Replace Text in Vim
Search and Replace Text in Vim

To search and replace text in all lines using the following substitute command:

:%s/am/was/g 

The text am will be replaced with the text was on all lines. The searched term does not need to be a complete word to be replaced.

Search and Replace All Text in Vim
Search and Replace All Text in Vim

To search and replace text in all Lines with user confirmation use the following command.

:%s/was/am not/gc

You will need to confirm the replacement of each text.

Search and Replace a Complete Word in Vim Editor

For instance, in our sample file, we have the word line appearing severally. We can invoke a search and replace command to look for only the complete pattern line on all lines and replace this complete word search with a text-like linear. The user will also be asked to confirm the replacement of each retrieved word.

 :%s/\<line\>/linear/gc 
Search and Replace Word in Vim
Search and Replace Word in Vim

The search pattern has to be a complete word or else we will be dealing with the error below.

 :%s/\<lin\>/linear/gc 
Pattern Not Found Error in Vim
Pattern Not Found Error in Vim

The word pattern teaches in the following command does not consider the case-sensitive nature of the word Teaches in the text file.

 :%s/\<teaches\>/Tutors/gci 
Search and Replace Case Insensitive in Vim
Search and Replace Case Insensitive in Vim

To search and replace with case sensitive use the following command.

 :%s/\<teaches\>/Tutors/gcI
Search and Replace Case Sensitive in Vim
Search and Replace Case Sensitive in Vim

We will end up with the above error.

However, if the search pattern is fixed to be case sensitive then the search and replace command should execute without any issue.

 :%s/\<Teaches\>/Tutors/gcI

We can now comfortably search and replace texts/words/phrases with Vim editor.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.