Linux Commands

Find and Replace Command in Vim

The majority of the Linux distributions preinstall Vim, a powerful and well-liked text editor. The majority of non-GUI operating systems use this command-line-based text editor. It has several features and is very configurable. There are built-in expression finds, a lightweight keyboard shortcut for nearly every function, and a strong, stable, and vibrant community of plugins. This post will discuss about the “find and replace”, one of its most helpful capabilities. You can find anything you want with a few configurations at lightning-fast speeds and with more precise criteria.

Syntax of Vim Find and Replace Function in Ubuntu 20.04

A useful command in Vim is a substitute, which is also available with the abbreviation “s”. We utilize this “s” with the colon “:”. The general syntax for the “substitute” command is as follows:

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

The command looks for a “pattern” on each line in [range] and substitutes it with a “string”. The current line is the one in which the pointer is situated. Let’s examine this syntax in detail. The text enclosed in square brackets ([]) is optional.

range: If you wish to replace the text between the specified lines, use the range clause of the syntax. It will just replace text on the existing line if a range is not specified.

pattern : The text you want to substitute is specified here.

string: The string used to swap out the pattern.

options: For each occurrence of the search pattern, the “g” flag should be utilized. If you wish the match to skip the case, you can indicate it with the “i” parameter. The “c” flag allows you to verify each find and replace the operation manually.

count: The number of lines that needs the text replacement.

How to Use Vim Find and Replace

To get the Vim editor, we need to install and configure the vim in our Ubuntu software first. Run the following command to install vim on the Debian-based Linux such as Ubuntu:

Once vim is installed on our system, we can configure it by just running the “vim” command as follows:

When the previous command is executed, the Vim editor is opened as in the following image:

To perform the find and replace operation, we need a test text file with data in it. Let’s start by making a document named “Story_Document.txt”.

After that, the Nano editor is then used to open it.

As we are in the Nano editor, we insert some lines of the text and save the modifications of the “Story_Document.txt” file.

Example 1:

The single slash “/” and dot technique is the most straightforward way to carry out a find and replace in the Vim editor. A word can be found by using the slash and replaced with a dot. Let’s use the Vim editor to open the “Story_Document.txt” file.

Pressing the forward-slash (/) key in the Vim editor allows us to search for the term “Alice”.

By using the Enter key, we move to the first instance of the term “Alice” which is highlighted in the following:

Next, we can enter the following cgn command combo. The last item we searched for is found by this Vim editor command which then deletes it and switches it to insert mode.

Let’s enter the term “Bella” while in the insert mode. The term “Alice” is swapped over for the word “Bella” in the first instance. To automatically replace the term “Bella” with the word “Alice”, we must hit the “N” key to move to the next instance of the word. The modifications should now result in the following in our file:

Hence, this is the easiest way to use the basic find and replace functionality in the Vim editor. Finding a word that occurs frequently would be a repeated and exhausting effort though.

Example 2:

To do both the simple and complex find and replace operations, we may also use the substitute command in the Vim editor. Let’s perform a find and replace on every occurrence on a single line.

This substitutes “Bella” for the word “Alice” at the beginning. Now, the “Story_Document.txt” file ought to look like this:

Example 3:

We must slightly alter the previous command to find and replace every occurrence on each line of the file:

We can replace any occurrences in any line by using the percent sign “%” which gives us an access to the entire file’s content. After replacing all the instances, the following should match the “Story_Document.txt” file:

Example 4:

When we use the replacement command to execute a find and replace, it is case-sensitive by default. By adding the “i” option to the end of our following query, we may conduct the searches that are not case-sensitive. We replace the occurrence of the word “rabbit” with “cat”.

The lines in the “Story_Document.txt” should now read as follows once we make the changes:

Example 5:

Additionally, there is a confirmation “c” option available for the substitution command. The find and replace command prompts us before a substitution is made when this option is included as the last parameter. The command asks for confirmation whenever the word “Alice” occurrence is found as follows:

We have the choice to press “y” for yes, “n” for no, or “a” for all occurrences if we receive a prompt. Pressing “l” replaces one line and “q” ends the current replacement.

The “Story_Document.txt” file is modified with the word “Bella”.

Example 6:

Both partial and complete matches are automatically found using the substitute command. Rather than changing the entire term, this enables us to change just a few words. The necessity to find an exact word and then substitute it could arise in some unique circumstances. Let’s find the precise term “bored” and swap out the term “sad” for it:

After running this program, the following should match in our “Story_Document.txt” file:

Conclusion

The many techniques for performing a find and replace comand in the Vim editor is examined in this article. Vim has a robust function called “find and replace” that makes it simple for you to swiftly edit your text. For short adjustments, the first approach is the most practical. For multiple modifications, it is constrained and laborious. The second option makes use of the substitute command in the Vim editor which provides us with greater freedom and is better suited for sophisticated searches and substitutions.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.