Vim, a powerful text editor, is a helpful tool in the Linux world with countless functionalities for text processing and manipulation. Two of its most useful functions are the finding and replacing commands. These commands boost the efficiency of users when they work on documents that contain lots of data. In this article, let us first learn how to use the vim find and replace commands and some real world applications.
Why would you require Vim Find and Replace?
Find and replace is mostly the second most used combination of functions after the legendary “copy and paste” when we are working with large documents and code. Vim, being known for its ability to improve efficiency of its users with its plethora of functionalities, offers very useful functions with its commands to perform operations without much learning curve involved. Understanding these commands like the find and replace command, will help in optimizing your day to day workload and augment productivity.
Basic Syntax
Command to Find
To find a word in a document, simply use the “/” command along with the pattern you would like to search for. For example, if there is a huge text file and you would like to find the word “Unixmen”, open command mode and execute /Unixmen. The first instance where Unixmen is mentioned will be highlighted. To highlight the next occurrence of the searched pattern, press “n”. To search backwards, replace / with ?. For example, if you are mid-way in a text file and would like to find the previous instance where Unixmen is used, open command mode and execute ?Unixmen. Similarly, if you would like to find the string “Unixmen” under the cursor, replace ? with asterisk *.
Command to Replace
In Vim, to replace a pattern with another, the syntax is:
Here, “replaced” is the word you want to remove, and “replace” is the word you want to put in the place of the removed word. For example, if you have a text file that mentions “Linuxmen” and you want to replace all those instances with “Unixmen”, the command you have to execute is:
In this vim replace command, the % symbol is used to instruct vim to search the entire file, “s” stands for substitute, and “g” means global replacement.
Confirm Before Replacement
While working with sensitive or important text files, it becomes important to confirm each replacement before replacing all instances. To confirm each replacement manually, you can add “c” at the end of the command, which means “confirm”.
Upon execution of this command, Vim will request your confirmation before replacing each instance and you can either approve or reject the replacement manually.
Replace only Whole Words
In some real life applications, it becomes important to replace only complete instances of a pattern. For example, if the intention is to replace all instances of the pattern “OS”, executing the Vim find and replace command without specialized parameters will replace the instances of “UbuntuOS” and also “CentOS”. For this case in Vim find and replace, you can use one of two methods.
If you want to set boundaries to avoid partial matches, specify \b to let Vim know the boundaries around the pattern to be found and replaced.
Executing this command in command mode replaces only the instances of “OS” with “OperatingSystem” and leaves all partial matches like “CentOS”.
To Find and Replace a String Across Multiple Files
If you are working with multiple files and you have to find and replace a specific string across multiple files, you need not open each file and execute the commands individually. With Vim find and replace, you can do so in any one of the two ways given below.
The first option in Vim find and replace is the “args” command. The args command performs the vim find and replace action in all the .txt files in the current directory. Here is the basic syntax:
Alternatively, the next method is to use the “vimgrep” command in Vim find and replace. The syntax is:
Important thing to note is that, unlike the args command, this command finds and replaces all instances across directory and its subdirectories.
Wrapping Up
Vim find and replace is a handy feature when you are working on large files and you are running short on time. With RegEx inputs, Vim find and replace becomes an even powerful tool for text processing.