Vim is a well-known free and open-source text editor for editing and manipulating all text and configuration files. The editing features of vim contain a large list, including changing, searching, and replacing/substituting the content in a file. It helps in making the file data more accurate as per requirements.
This post explains how to change, replace, and substitute the string in a vim text editor.
- Using the Slash(\) and Dot(.) Operators
- Using the Substitute Command
- Using the Line Number
Method 1: Using the Slash(/) and Dot(.) Operators
The “slash and dot” operators are the basic and simplest way to search and replace the string/pattern in the Vim editor. The “slash” operator specifies the searched word, while the “dot” operator replaces the searched word with another.
Let’s understand this concept with an example:
Example:
A sample file “File1.txt” is opened in the “vim” text editor that contains the following lines of information:
$ vim File1.txt
The vim is in its default “normal” mode that contains the text.
Search the Word
Press the backward slash(/) in normal mode and write the string/word that the user wants to search, i.e., “/modify.” It highlights the first occurrence of “modify”:
Delete the Searched Word
Type the “cgn(change the next search hit))” keystroke, and it deletes the searched word and enters into the “INSERT” mode:
Replace Word
Type the new word in place of the deleted searched word that is “improve” in this case:
Press the “Esc” key to finish the process.
Replace Next Occurrence Simultaneously
Press the “.(dot)” key from the keyboard to change the next occurrence simultaneously in the whole file:
The word “modify” has been replaced/changed with “improve.”
Method 2: Using the Substitute Command
The vim editor offers substitute command (s) to perform basic to advanced search and replace(r) operations through a single command. Its working depends on its basic syntax that is stated here:
Syntax:
:s/<search-pattern>/<replace-pattern>/option
The generalized syntax specifies the “search” pattern and substitutes (s/) it with the replace(r) string.
The supported options of the substitute command are listed below:
Options
- g: Substitutes the new replaced string globally.
- c: Asks for confirmation before substitution.
- i: Ignore the case sensitivity, i.e., LINUX/linux/LiNux, and so on.
Let’s move on to the practical implementation of the substitute command.
Example
Another sample file, “File2.txt,” is opened in vim having the following content:
$ vim File2.txt
Press the Esc key to enter into the “command mode.”
Substitute Globally
To globally substitute a search pattern(RHEL) with the replace(CentOS) by executing this below command:
:%s/RHEL/CentOS/g
The command is written at the bottom of the “vim” editor. Press the Enter key to execute the command:
The output shows that the searched word “RHEL” has been replaced globally with “CentOS.”
Substitute Globally(Ask Permissions)
Use the “c” permission option with the “g(globally)” for asking confirmation before substituting the specified word:
:%s/RHEL/CentOS/gc
Hit the “Enter” key:
Enter “Y” for yes, and “n” for no. In this scenario, Y is pressed for yes to substitute globally:
The “RHEL” has been substituted with another word “CentOS”.
Substitute Globally(Ignore Case Sensitivity)
The “i” flag ignores the case sensitivity that is by default enabled in the vim editor. For instance, the searched word is “Ubuntu,” and the available word in the file is “uBuntu”:
:%s/Ubuntu/Fedora/gi
Execute the command and see the result:
The word “uBuntu” has been replaced with “Fedora” by ignoring the case sensitivity.
Method 3: Using the Line Number
The vim text editor supports the “set number” command to display the line number in the text file. It helps in performing the search and replace operation in a specific line of a file. Let’s see how it can be done:
Set the Line Number
The “File2.txt” is taken as an example. Enables the command line mode using the “Esc” key and enter the following command:
:set number
The above command has successfully set the numbering of each line.
Search and Replace the Word
The general syntax to search and replace the word in a specific line is written here:
:<start_line>,<end_line>s/<search_term>/<replace_term>/g
In the above syntax, the “g(–global)” is an optional parameter that replaces the string in the whole file.
Use this syntax to search the “Debian” word from the line “4-5” and replace it with “Arch” in this way:
:4,5s/Debian/Arch
In Lines 4 and 5 the word “Debian” has been replaced with “Arch”.
Conclusion
The Vim editor performs the change, replace, and substitute operation using the “Slash(\) and Dot(.)” operator, the “Substitute” command, and “Line Number.” The “Substitute” command replaces the specified string globally or in a particular line of a file. In addition, the “Line Number” built-in feature of Vim can also be used to perform this task at a specific range of lines.
This post has covered all possible methods to change, replace and substitute in the Vim text editor.