How to Find and Replace in Vim and Vi

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.

Basic Find and Replace Commands

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
old-new

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
old-d

This command will search for all lines that contain the word “old” and delete them.

Advanced Find and Replace Techniques

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.

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
old-new

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.

Real-World Examples and Use Cases

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.

Conclusion

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.

FAQ

How do I perform a simple find and replace in Vim or Vi?
How do I find and replace text in a specific range of lines?
How can I replace text only on the current line?
How to find and replace text with confirmation prompts?
How do I perform a case-insensitive find and replace?

Related Post

How to Check if Your Linux OS is 32-bit or 64

In the field of computing understanding your syste...

How to Install Nucleus CMS in Linux

Nucleus CMS is a free, open-source software designed fo...

Explore the man (Manual) Command in Linux

The man command, short for “manual”, is an essentia...

How to Check Running Processes in Linux?

In Linux, you can efficiently manage your system by che...

How to Install Rancher on CentOS 7

Rancher is an open-source platform that simplifies the ...

How to Create a Bash Script: Step-by-Step Gui

Bash scripts are powerful tools that allow you to autom...

Leave a Comment