Find and Replace in Vim Editor

Find and Replace in Vim Editor

In this tutorial, we will learn how to find and replace any text in Vim editor. Vim is the most widely used editor, in many Linux distributions, the vim editor is already installed on the server. You can use this tutorial in any of the Linux distributions like Ubuntu, CentOS, Debian, etc.

You can install the Vim editor with the following command.

For Ubuntu/Debian

apt install vim -y

For RHEL/CentOS

yum install vim -y

Basic Find and Replace

In Vim editor, we can easily find and replace by using the :s command.

Vim editor has various modes like Insert mode, Visual mode, Normal mode, etc. This command will only work in the Normal mode. To switch to Normal mode, just press the “Esc” key.

The syntax of the command is as follows:

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

[range] – To define the range in which you want to search for the pattern.

{pattern} – Define the pattern(String/Number) which you want to search in the file.

{string} – Define the string from which we have to replace the searched pattern.

[count] – It is used to multiply the command.

While executing the command, if we have not defined the [range] and [count] then only the pattern found in the current line will be replaced. The current line is the line where the cursor is placed.

For example, To search and replace the first occurrence of the string “User” with “Linuxpanda” in the current line. Run the following command.

Read More: Setup OpenVPN client for Windows

:s/User/Linuxpanda/

If the current line has multiple occurrences of the searched pattern then to replace all the occurrences use g the flag with :s attribute. Run the following command to replace all the occurrences.

:s/User/Linuxpanda/g

To search and replace the pattern in the entire file, then we have to define the [range] as ” % “.  The character % defines the range from the First line to the Last line of the file.

:%s/User/Linuxpanda/g

To delete all the matched patterns, leave the string part empty. The following command will delete all the matched patterns from the current line.

:s/User//g

To delete all the matched patterns from the entire file, leave the string part empty. The following command will delete all the matched patterns from the entire file.

:%s/User//g

If the pattern or the replacement string has / in it, then we have to use the symbol ! instead of the / symbol.

:s!Pattern!String!g

For example, we have to replace /home/user1/ with /home/user2/. Run the following command to replace it.

:s!/home/user1/!/home/user2/!g

If we want that the command asks for confirmation before replacing the pattern, use c the flag.

:s/User/Linuxpanda/gc

The output will look like this:-

replace with Linuxpanda (y/n/a/q/l/^E/^Y)?

Press y to replace, n to skip the match, a to replace all the matches, q to quit, l to replace the first match and then quit, ^E (CTRL + E) to scroll up  and ^Y (CTRL + Y) to scroll down.

We can also use a line or a regular expression as a pattern.

For example, we have to replace all the lines starting with ” hi” and Replace it with ” hello user “

:%s/^hi.*/hello user/gc

The ^ (caret) a symbol used to match the beginning of a line and .* is used to match any number of characters.

Note: The above command will search the line which starts with “hi” and replace the whole line with “hello user”.

Case Sensitivity

In Vim editor, the search operation is case-sensitive. Like we are searching for “Linuxpanda” will not match with “linuxpanda”.

To ignore the case, use i a flag while searching for the pattern.

:s/User/Linuxpanda/gi

Search Range

When a range is not defined, then the substitute command will only work on the current line.

We can easily define the range with , symbols. For example, we have to replace all occurrences of “user” with “linuxpanda” from lines 2 to 8 then run the following command.

:2,8s/user/linuxpanda/g

The range is inclusive, it means that the line second and eight lines are included in the range.

If we want to define the range from the current line to the last line. In this case, we use . to define the current line as the starting point and $ to define the last line.

:.,$s/user/linuxpanda/

We can also specify the range by using the ” + ” and ” – ” symbol. By adding or subtracting from the preceding line number. By default, Vim editor starts from the 1st line. If we omit to mention the starting point, then Vim editor defines it as line number 1.

For example, we have to replace all occurrences of “user” with “linuxpanda” from the current line to the next 6 lines.

:.,+6s/foo/bar/g

Substituting Whole Word

The substitute command search for a pattern as a string, not as a whole word, like we are searching for “linux”. The search finds matches.

linux
linuxuser
linuxpanda

In this case, if we replace the word “linux” with “abc”. The word “abc” replaces the “linux” from every word. The result will look like

abc
abcuser
abcpanda

To search for a whole word, use \< to define the beginning and \> define the end of the word.

For example, we want to search the word “linux”, use the following command.

:s/\<linux\>/abc/

The output will look like this:-

abc
linuxuser
linuxpanda

Substitute History

Vim editor track all the records of the command of the current session. To check these records use :s an attribute with an up/down arrow key, It will show the previous substitute commands. We can easily edit and execute the previous substitute commands.

Useful example:-

Comment line from 2 to 10:

:2,10s/^/#/

To uncomment line from 2 to 10:

:2,10s/^#//

Replace all occurrences of ‘user1’, ‘user2’, and ‘user3’ with ‘linuxuser’:

:%s/user1\|user2\|user3/fruit/g

To remove whitespace from the end of each line:

:%s/\s\+$//e

Conclusion

In this Find and Replace in Vim Editor tutorial, we learned how to install vim and how to find and replace any string in the entire file very easily. In a future tutorial, we will discuss more of these types of Vim attributes.

If you guys have any queries related to this tutorial then let me know in the comment section.