Or copy link
Copy link
Vim and Vi are two of the most popular text editors in the world, known for their flexibility, customization, and efficiency. One of the most powerful features of these editors is their find-and-replace functionality, which allows users to search for specific patterns and replace them with new text.
In this article, we’ll dive deep into the world of find and replace in Vim and Vi, covering the basic commands, advanced techniques, and shortcuts that will take your productivity to the next level.
Before we dive into the advanced techniques, let’s start with the basics. The most common find and replace in Vim and Vi is the :s command.
The :s Command
The :s command is used to search for a pattern and replace it with new text. The basic syntax of the :s command is as follows:
:s/pattern/replacement/
Here, pattern is the text you want to search for, and replacement is the text you want to replace it with.
For example, let’s say you want to replace the occurrence of the word “old” with “new” from a current line in a file. In this case, you have to use the linux command find and replace for this purpose. You can use the following command.
:s/old/new/g
The g at the end of the command is optional, It’s essential if you want to replace all occurrences of the pattern, not just the first one from the current line.
In case, you want Vim replace all occurrences from the whole text, you can use the below command:
:%s/old/new/g
The % symbol is a shortcut for specifying all lines in the file. Similarly, if you only want to replace the first occurrence from the current line then you can use the following command:
:s/old/new/
The :g Command
The :g command is similar to the :s command, but it’s used for global search and replaces Vim. The basic syntax of the :g command is as follows:
:g/pattern/command
Here, the pattern is the text you want to search for, For example, let’s say you want to delete all lines that contain the word “old”. You can use the following command:
:g/old/d
This command will search for all lines that contain the word “old” and delete them.
Now that we’ve covered the basics, let’s move on to some advanced techniques.
Regular Expressions (regex)
Regular expressions, or regexes, are a powerful tool for matching patterns in text. Regex patterns are used in various Vim and Vi commands, including the :substitute command, to search and replace text.
Enhance your Linux environment’s efficiency today!
Ultahost provides Linux hosting with NVME SSD storage. Streamline your workflow by configuring environment variables on our Linux VPS.
In Vim and Vi, regex patterns are enclosed in forward slashes /. For example, the following command searches for all occurrences of the word “old” followed by one or more digits:
:%s/\<old\>/new/g
Using Patterns and Character Classes
Patterns and character classes are essential components of regex. A pattern is a sequence of characters that you want to match, while a character class is a set of characters that you want to match. In Vim and Vi, character classes are enclosed in square brackets [].
For example, let’s say you want to replace all occurrences of the word “old” followed by a digit or a letter:
:s/old[0-9a-zA-Z]/new/
Here, old is a pattern that matches the literal string “old”, and [0-9a-zA-Z] is a character class that matches any digit (0-9), lowercase letter (a-z), or uppercase letter (A-Z).
Using Groups and Backreferences
Groups and backreferences are advanced regex concepts that allow you to create complex patterns and replace them with new text. In Vim and Vi, groups are enclosed in parentheses ().
A group is a part of a regex pattern that you want to capture and use later. You can then refer to the captured group using a backreference. In Vim and Vi, backreferences are denoted by a backslash followed by a number, such as \1, \2, etc.
For example, the following command searches for all occurrences of the word “old” followed by one or more digits, captures the digits, and replaces them with “new” followed by the captured digits:
:s/old(\d+)/new\1/
Here, (\d+) is a group that captures one or more digits, and \1 is a backreference that refers to the captured group.
Read also How to Create a Bash Script: Step-by-Step Guide
Now that we’ve covered the basics, advanced techniques, and shortcuts, let’s move on to some real-world examples and use cases.
Find and Replace in Code
Imagine you need to update a deprecated function in your codebase. Instead of manually searching and replacing each instance, you can use regex to do it efficiently:
:%s/old_function/new_function/g
This command finds all occurrences of old_function and replaces them with new_function throughout the entire file (% and g flags).
Find and Replace in Writing and Editing
Vim Find and Replace all is not just limited to coding; it’s also essential in writing and editing. For example, If you’ve made a recurring typo or grammatical error throughout a document, regex can provide a quick fix:
:%s/old phrase/new phrase/g
This command will search for all occurrences of the old phrase and replace them with new phrases.
In this article, we’ve explored the powerful find and replace functionality in Vim and Vi. We started with the basics, covering the :s and :g commands, and then delved into advanced techniques such as using regular expressions, groups, and backreferences. We also examined real-world examples and use cases, including finding and replacing code, writing, and editing. Additionally, we covered common mistakes and troubleshooting tips to help you avoid errors.
You can significantly improve your productivity and efficiency by mastering the find and replace functionality in Vim and Vi.
Mastering the Replace function in Vim and Vi can greatly improve your text editing efficiency. Coupled with the flexibility and performance with Ultahost a Fully managed dedicated server, you can achieve a powerful and reliable workflow for your development needs.
To perform a simple find and replace, use the :%s/old_text/new_text/g Command. This command will replace all instances of old_text with new_text throughout the entire file.
You can specify a range of lines by using line numbers or relative lines. For example, to replace text between lines 10 and 20, use :10,20s/old_text/new_text/g
To replace text only on the current line, use: :s/old_text/new_text/g
To be prompted for confirmation before each replacement, add the c flag at the end: :%s/old_text/new_text/gc
To ignore a case during the find and replace, use the i flag: :%s/old_text/new_text/gi
In the field of computing understanding your syste...
Nucleus CMS is a free, open-source software designed fo...
The man command, short for “manual”, is an essentia...
In Linux, you can efficiently manage your system by che...
Rancher is an open-source platform that simplifies the ...
Bash scripts are powerful tools that allow you to autom...
Save my name, email, and website in this browser for the next time I comment.
Δ