How to Find and Replace Text in Vim? [5 Cases]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Follow the below commands to find and replace text in Vim:

  1. To find and replace all occurrences, use the syntax: :%s/original/replacement/g
  2. To find and replace a word ignoring case, use the syntax: :%s/original_phrase/replace_phrase/gi
  3. To find and replace a word after confirmation, use the syntax: :%s/original/replacement/gc
  4. 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]
EXPLANATION

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:/eval command.Now, type cgn to delete the “eval” word which is the most recently searched word:"cgn" deleted a word in Vim.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:"echo" present in the file.Now type :%s/echo/hello/g and press the ENTER button:The "hello" replaced "echo" in Vim editor.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:Command has found "echo" and they have been replaced by "hello" in Vim ignoring casing.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:"hello" has replaced "echo" after confirmation in Vim.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.

vim is replacing found matches after the confirmation from the userThe image shows that “hello” has replaced all “echo” after asking for confirmation.

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:Command has found "echo" and it has been replaced by "hello" in Vim.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:

  1. 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
  2. Put +/- to add or subtract lines from the current line. The correct syntax is  :.,+/-<line number>s/original_phrase/replace_phrase/g
  3. Put “\c” after the search pattern to do the search case sensitive. The correct syntax is :%s/ original_phrase\c/replace_phrase/g
  4. 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.
  5. To eliminate double space, follow the syntax :%s/[double space]/[single space]/g
  6. To replace the different words with a single word, follow the syntax :%s/\(word1\|word2\)/This text editor/g
  7. 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


<< Go Back to Vim in Linux | Linux Text Editors | Learn Linux Basics

Rate this post
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment