Follow the below commands to find and replace text in Vim:
- To find and replace all occurrences, use the syntax:
:%s/original/replacement/g
- To find and replace a word ignoring case, use the syntax:
:%s/original_phrase/replace_phrase/gi
- To find and replace a word after confirmation, use the syntax:
:%s/original/replacement/gc
- To find and replace a word from a specific range of lines, use the syntax:
:start_line_number, end_line_number s/<original>/<replacement>/g
In this article, I will explore these approaches to finding and replacing text in Vim. So without further delay, let’s get started.
How Vim Substitute Command Works?
The substitute command looks for a specific pattern in the file and replaces it with a predefined text. The syntax for the Vim substitute command is:
:[range]s/original_phrase/replace_phrase/options [count]
Here, [range]
is optional. It specifies the range of specific lines where substitute operation takes place. After that, the s
stands for substitute. It searches the original_phrase
and replaces it with replace_phrase
.
There are three options:
- c: Asks for confirmation before deleting.
- i: Ignores the case.
- g: Replaces all occurrences.
Finally, [count]
is optional. It specifies the number of occurrences to replace on each line.
5 Cases of Find and Replace in Vim
Here, I have listed 5 cases of finding and replacing text/pattern in Vim. Check them out:
1. Basic Search and Replace Using Slash (“/”) and “cgn” Command
Vim editor finds a specific pattern with the help of the slash “/” command. And, the cgn
command can usually delete the most recently searched word. Here, I will delete the “eval” word of the current line (where the cursor is placed). For this, I will type /eval
. It will mark the first match found after the cursor:Now, type cgn
to delete the “eval” word which is the most recently searched word:To repeat the search and action, press .
.
2. Find and Replace All Occurrences Using Substitute Command
To find and replace all occurrences of a keyword in all lines, first switch to the normal mode. Then type :%s/original/replacement/g
. Here I will replace all found “echo” with “hello”. To do this, first I will switch to the normal mode:Now type :%s/echo/hello/g
and press the ENTER button:The image shows that “hello” has replaced all “echo” in this entire file.
Note: To replace all occurrences of a keyword in the current line, first place the cursor to the intended line in normal mode. Then type :s/pattern/replacement/
and press ENTER.
The substitute command matches with the full word or a part of a word by default. To set the match to the exact word or whole word type :s/\<whole word\>/replacement/gi
in the normal mode and press the ENTER button.
3. Case-Insensitive Find and Replace
To perform case insensitive find and replace operation, first switch to the normal mode. Then type :%s/original_phrase/replace_phrase/gi
and press the ENTER button. It will replace the original phrase with a replace phrase ignoring the casing.
In this example, I will replace all “echo” words with “hello” using :%s/ECHO/hello/gi
command:The image shows that all “echo” has been replaced by “hello” ignoring the casing.
4. Find and Replace With Confirmation in Vim
To find and replace with confirmation in Vim, type :%s/original/replacement/gc
and press the ENTER button from the normal mode. It will ask for confirmation before replacing “original” with “replacement”.
In this case, I will replace “echo” with “hello” with confirmation by typing :%s/echo/hello/gc
command from the normal mode:It will ask for confirmation on every occurrence. To confirm, press y
.
Note: Press “n” for no, and “a” for all occurrences, “q” to quit, or “l” to replace only one line.
5. Find and Replace in Vim Within Specific Range of Lines
To find and replace in Vim within a specific range of lines type :start_line_number, end_line_number s/<original>/<replacement>/g
from normal mode and press the ENTER button. It will search for the original within the specific line range to replace.
Here, I will find and replace the “echo” with “hello” within the line range 8 to 9. For this, I will type :8, 9 s/echo/hello/g
and press ENTER:The image shows that the “hello” has replaced “echo” from the line range 8 to 9.
Special Cases of Find and Replace Text in Vim
Keep these points in mind to use the Vim editor efficiently:
- Put dot (.) and dollar sign ($) to substitute everything from the current line to the last line. The correct syntax is
:.,$s/original_phrase/replace_phrase/g
- Put +/- to add or subtract lines from the current line. The correct syntax is
:.,+/-<line number>s/original_phrase/replace_phrase/g
- Put “\c” after the search pattern to do the search case sensitive. The correct syntax is
:%s/ original_phrase\c/replace_phrase/g
- To replace an old with a new line, follow the syntax
:%s/^Old line begins .*/ New line!/g
. Here, caret “^” matches the beginning of a line, and “*” matches the rest of the line. - To eliminate double space, follow the syntax
:%s/[double space]/[single space]/g
- To replace the different words with a single word, follow the syntax
:%s/\(word1\|word2\)/This text editor/g
- To delete a word from the entire file, omit the replace_phrase and follow the syntax
:%s/word_to_be_deleted/
Conclusion
In this article, I have explored different aspects of find and replace in Vim. I hope this will be a helpful guide for the Vim editor users for efficient file operation. Don’t miss to comment below with your valuable thoughts about this article.
People Also Ask
How to replace string inside Vim?
To replace string inside Vim, first switch to the normal mode by pressing Esc button. Then execute :%s/original_string/replace_string/g
command.
How to replace empty line in Vim?
To replace the empty line in Vim, execute :g/^$/d
command in the normal mode. It will remove all empty lines in Vim.
How do I change multiple words in Vim?
To change multiple words in Vim, execute :%s/\(word1\|word2\)/new word/g
. It will replace word1 and word2 with new words.
How do you replace selected text in Vim?
To replace selected text in Vim, type Ctrl+r in visual mode. It will ask to enter text to replace the selected text.
How to check substitute history?
To access the previous substitute history in Vim, type :s
and use the up arrow and down arrow to find the command previously used by the users. Then press the ENTER button to execute the command after selecting and editing the intended command.
Related Articles
- How to Copy, Cut, and Paste Text in Vim? [All Cases]
- How to Search Text in Vim Editor? [8 Cases]
- How to Undo and Redo Changes in Vim? [All Ways]
<< Go Back to Vim in Linux | Linux Text Editors | Learn Linux Basics
FUNDAMENTALS A Complete Guide for Beginners