Text editors are some of the essential tools for us developers, and having powerful and capable features, even simple tasks such as search and replace, can be invaluable.
Vim stands out to be one of the most potent and excellent text editors of the modern age. Yes, it is Vim over Emacs over here.
And as you can guess, some of its power comes from the ability to perform highly complex text searches and transformations from a single execution. Hence, Vim's search and replacement power is unparalleled.
Let's get to it, shall we?
The Basics
Let us start with what you should know if you are new to Vim or its family of editors. Vim search and replace command is abbreviated by colon s :s
or :substitute
.
The full syntax is as shown:
:[range]s/pattern/replacement/[flags]
Where the parameters are described below:
- range - specifies which lines to act upon.
- pattern - the search pattern or the string you wish to find/search.
- replacement - this defines the text you wish to act as a replacement for the matching patterns.
- flags - control how the replacement is performed.
Okay, now that we have the basics out of the way, let us explore more in-depth examples of how to use them.
Basic Search and Replace
And as you can guess, we will start with the fundamentals by learning how to search for an occurrence and replace it with a given string.
For example, suppose we have an entry as shown:
/dev/rdisk3s3: fsck_apfs completed at Mon Aug 14 23:35:06 2023
To search and replace the occurrence of the string 2023
and replace it with 2024
, we can start by entering the command mode in Vim by pressing the ESC
key.
We can run the command as shown:
:s/2023/2024
Note: This will replace the first occurrence of the matching pattern. Ensure to position the cursor on the line you wish to search. Otherwise, Vim will fail to find the pattern by default.
Replace All Occurrences in a Line
However, if you wish to replace all the occurrences in a given line, you can use the g
option, which tells Vim that you wish to perform a global replacement.
An example is as shown:
:s/2023/2024/g
Replace Across Multiple Lines
In other cases, you may need to replace a matching occurrence across multiples. Luckily, vim allows us to specify the range we wish to span the replacement operation using numerical values.
For example, if we specify the range to be 1,3
, it tells Vim that we wish to perform the search and replace operation from line 1 to line 3.
:1,3s/2023/2024/g
Replacing on Entire File
To act on the entire file, you must use the %
symbol as:
:%s/2023/2024/g
Replacing with Confirmation
If you want to confirm each replacement, you can use the -c flag in the command as shown:
:%s/2023/2024/gc
Be careful about this if you are operating on a large file. it can quickly become cumbersome and inefficient. Consider other automation tools, such as Ansible, if you find yourself doing this.
Performing a Case-Insensitive Search
By default, Vim will adhere to the casing specified in the pattern. This ensures that non-matching casing is not replaced by accident.
However, you may need to replace all occurrences despite the casing. for that, you can use the i
parameter as shown:
:%s/casingIrrelavant/replaceme/gi
Search and Replace Special Characters
You may also encounter instances where you must find and replace text with special characters such as .
, *
, ^
, and $
). In such a case, you can escape them using a backslash character as shown (\
).
:%s/2023\./2024./g
And there you have it.
Conclusion
In this tutorial, we learned how we can leverage the powerful features of Vim to perform search and replace operations in a given file. We started with the basics and covered more complex searches, including how to replace special characters.