How to Find and Replace Using Vim and Vi in Linux

Vim and Vi is the most commonly used text editor in Linux. You can easily find and replace text in Vim and Vi in Linux with its substitute commands. Additionally, it is preinstalled in most Linux distributions, and using it is quick and efficient. 

This article discusses and describes how you can search and replace text using Vim and Vi in Linux in various ways. Furthermore, it also lists some additional commands that can be used with the Vim editor.

0. Install Vim and Vi

Although the Vim and Vi command is preinstalled in most Linux distributions, you can install it easily if you do not find it in your system. To install the Vim editor, execute the command given below, or download it from its official website.

sudo apt install vim

Additionally, you can create a file with Vim and Vi or edit an existing file. If the file exists in your system, then skip step 1 and head over to step 2. Otherwise, follow through the step 1. 

1. Create a File With Vim and Vi

The Vim and Vi commands can be used to create as well as open a file. The syntax is:

vim FILE_NAME

Where FILE_NAME is the target file name that you want to create. For example:

vim helloworld.txt

At this stage, it is important to know that Vim and Vi have three important modes:

  • Normal: For navigation and simple editing
  • Insert: For explicitly inserting and modifying text
  • Command Line: For operations like saving, exiting, etc.

After you press the “Enter” key, you will be in Normal mode. Additionally, to ensure that you are in the Normal mode, hit the “Esc” key. This will confirm that you are in Normal mode and ready to use Vim and Vi for simple editing. Your screen will look something like this:

create a file using the vim and vi command

Now, type “:q!” and hit the “Enter” key. This will enable you to exit from the file without saving any changes. Furthermore, the colon (:) symbol switches the Normal mode to the Command Line mode. And the letter “q!” quits the editor.

After that, we will try to write something in the file. Open the file using the Vim command. For example:

Vim helloworld.txt

First, press the “Esc” key to ensure that you are in the Normal mode. After that, press the letter “i” to enable Insert mode. When you are in the insert mode, you will see -- INSERT -- written at the bottom left of the screen.

For instance:

Write using the Vim and Vi command

To save the content that you have typed, hit the “Esc” key first. After that, type colon (:) followed by the letter “X” and “!”. Press the “Enter” key. 

At last, you have saved your file successfully!

2. Basic Find and Replace Using Vim and Vi in Linux

You can perform basic find and replace in Vim and Vi. To perform a basic search and replace, use the colon (:) symbol, followed by the letter s. This is also called the “substitute command”. 

The general syntax of the substitute command consists of various parameters. These parameters include range, pattern, string, flag, and count. For example:

:[range]s/{pattern}/{string}/[flags] [count]

The substitute command searches for the {pattern} in each line stated in [range]. After the search, it replaces the {pattern} with a {string}. In the general syntax, [count] is a positive integer that multiplies the command. The [range] and [count] parameters are optional. If you do not mention them, the editor will only replace the text in the current line where the cursor is visible.

This is our sample text in the .txt file. 

Find and replace using the Vim and Vi command

For example, if you want to search where the pattern ‘Hello’ occurred for the first time in the current line and replace it with ‘hello’, you would type:

:s/Hello/hello/

As you can see from the image given below, the text is replaced successfully.

Find and replace using the Vim and Vi command

Furthermore, you can also dd the “g” flag at the end of the command if you want to find and replace all the occurrences of any pattern in the current line. For example: 

:s/Hello/hello/g

If you omit the {string}, the editor will consider it empty and delete the searched pattern. For example:

:s/Hello//g

Moreover, to confirm that the editor has replaced all the occurrences of a string, use the “c” flag. For example:

:s/Hello/hello/gc

Regular expressions can also be used to find and replace Vim and Vi commands. For example, the command given below will replace all the words starting with  “te” with “world”. 

:%s/^te.*/world/gc
Find and replace using the Vim and Vi command

The caret (^)  symbol indicates the beginning of a line and .* matches any number of characters after that string.

3. Disable the Case Sensitivity to Find and Replace Using Vim and Vi

The search operation in Vim and Vi command is case-sensitive by default. The string “Hello” will not match “hello”.  To disable the case sensitivity, use the “i” flag after the “g” flag. For example: 

:s/hello/world/gi

This command will search for all the occurrences of the string “hello”, whether it starts with capital “H” or small “h”. 

4. Search Range

If you do not define the range in the substitute command, it will only operate on the single current line. However, if you want to search and replace the pattern between two o more lines, you can specify the range using the comma (,), or semicolon (;) before the letter “s” in the substitute command. 

For example, to replace all the occurrences of “world” with “hello” in lines 3-5, you will type: 

:3,10s/hello/world/g

The range includes the third line as well as the fifth line. 

Search range

Furthermore, there are various symbols that you can use to address the range.

  • The dot (.) character: Current line
  • The $ sign: Last line

5. Substituting Word Instead of Pattern

The substitute command usually looks for patterns of the strings instead of the whole word. If you want the command to search for the word instead of the pattern, enclose the word in the \< \>  symbol. For example: 

:s/\<hello\>/world/

6. Some Additional Examples to Find and Replace in Vim and Vi command in Linux

CommandsDescriptions
:%s/hello/world/Replace the first occurrence of “hello” with “world” on each line
:%s/.*\zshello/world/Replace the last occurrence of “hello” with “world” on each line
:%s/\<hello\>//gDelete all occurrences of the whole word “hello” on each line
:%s/\<hello\>.\{5}//Delete the first occurrence of the whole word “hello” and the following five occurrences
:%s/.*\<hello\>//Delete the whole word “hello” and all preceding text (from the beginning of line) on each line
:%s/.*\ze\<hello\>//Delete all the text preceding the whole word “hello” (from the beginning of line) on each line

In this article, we have covered how to find and replace using Vim and Vi commands in Linux. In conclusion, you can perform search and replace using various options with substitute commands in Linux. Find and replace is a powerful feature of the Vim and Vi command and allows you to make changes to your text efficiently.

Feel free to leave a comment if you have any questions.

Leave a Reply
Related Posts