Finding a word or pattern in a long list is difficult to do, however, we can find and replace in Vim using the built-in shortcut keys. The find command saves a lot of time to get the desired word or pattern in the document and can be replaced with a single command. This article will assist in searching and finding different ways in Vim.
Let’s discuss it!
How to Find and Replace in Vim?
Several methods are used to replace and find in Vim. This guide will discuss the two methods to find & replace:
Let’s explain the first method.
Using Find (/) and Replace (.) Shortcut keys
The simplest method to search and replace is using (/) to find and (.) to replace. Let’s discuss the steps for this method.
Step 1: Open the File in Vim
To open the file in Vim, utilize the “vim <FileName>” syntax. For instance: to open the “testfile1.txt”, execute this command in the terminal:
$ vim testfile1.txt
Step 2: Search a Specific Word/Pattern
Press the forward slash “/” key to tell the editor to search this word/pattern. Search the desired word (in my case: line) after the “/”and press enter:
Note: The highlighted word indicates the current cursor position. Press “n” or “N” to move to the next occurrence.
Step 3: Enter the Replacement Word/Pattern
Press the “Esc” key for the normal mode, and press the “c”, then “g”, and “n” keys to remove the word which we want to replace:
Now, enter the new replacement word; for instance, the arrow word is used below:
Step 4: Replace the Next Occurrences
Press the “Esc” for Normal mode. Now, press “n” or “N” for the next occurrences in the document. Move the cursor to the specific occurrence and press the dot (.), and the word will be replaced:
This way, we can replace a single word in the document using the shortcut keys.
Note: The above steps will be followed for every replacement method, and the shortcut keys should be entered in the Normal mode (Pressing the Esc key).
Using Substitute Command
The substitute command finds and replaces a word/pattern in Vim. Let’s discuss the syntax and options for substitute commands before moving to examples.
The substitute command is used to replace in Vim, whose basic syntax is as follows:
Range: Shows the from a specific line to another like from ”1,4”.
Search-word: Vim will search this pattern/word for replacement.
Replace-word: The matching pattern/word will be replaced with this word.
Options: Set the options with any of the substitute command built-in options.
Count: This count means the specific number of lines, like “2”.
Options for Substitute Command in Linux:
The options for substitute command is listed down:
|g||This option replaces all the occurrences in a line/document.|
|c||This option enables you to show a confirmation prompt.|
|i||To make the search case insensitive.|
|$||Represents the current line.|
|%||Shows the entire document|
Example 1: Replace a Word in the Current Line
We can use the substitute command to find and replace the word (at first occurrence) in the current line. Let’s find and replace in the below-mentioned line:
Enter the below command to replace “new” with the “sample” word:
The output displays that the “new” word is replaced with the “sample” word.
Example 2: Replace at All Occurrences in Current Line
The “g” flag of the substitute command is used for all the occurrences. The current line will all “sample” word occurrences will be replaced:
To replace the “sample” word at all the occurrences in the current line with “upcoming”, utilize the following command and press Enter to make changes:
Example 3: Replace in the Entire Document
The “%” option represents the entire document. Let’s replace the “test” word throughout the entire document:
To replace “test” at all the occurrences in the entire document with “sample”, execute the following syntax and click “Enter”:
Example 4: Replace the Entire line / Pattern with Space
We can delete or replace it with whitespace in Vim. Let’s replace “sample” word in the indicated line:
To replace the “sample” with a blank space, use the below syntax, and click Enter keyto replace:
The specified word will be replaced with empty space(deleted).
Example 5: Replace with Confirmation Prompt
The “c” option shows a prompt before deleting a word/line in the specified line.
To get a confirmation prompt before replacing a word, for instance: “new” with the “upcoming” word, execute this in normal mode:
The confirmation prompt (as shown in the above output) will appear, enter “y” or “Y” to replace:
Y: Replace the specified match with Y.
n: Skip to the next match with n.
a: Replace all matches with a.
q: Exit the editor with q.
I: Replace match & quit the editor.
Confirm by entering “y” will replace with “new” word with the “upcoming” word.
Example 6: Replace with Case Insensitivity
The Vim editor is case-sensitive like the terminal. To make the Vim editor case insensitive, utilize the “i” option with the replacement command:
For example, to replace “SenSitive” with “based” in the whole document without any case sensitivity, use this:
Example 7: Replace within Specific Lines or Range
We can replace the text within a specified range as highlighted below:
For example, to replace the word “text” with “new_text” from lines 3 to 6, use the following:
Example 8: Replace a Word in Two Lines
We can replace a word in the specific number of lines from the current line/cursor position by using the specific number (like 2) with the command. Let’s replace in the indicated lines below:
To replace the word “sample” with “new_sample” in the current and next line (total 2 lines), use this:
Example 9: Replace Multiple Words With a Common Word
Multiple words can be replaced with a common word in Vim. Let’s replace the below highlighted words with a common word, “content”:
To replace multiple words, “text” and “sample”, with a common word content, in the entire document, use this command:
That’s all from this post!
We can find & replace with a substitute command or find the word with the forward slash “/” and replace it with the dot sign “.”. The substitute command is used to find and replace the word/pattern in different ways. This post has briefly explained the methods to find and replace in Vim.