Mastering Search and Replace in Vi Editor
Last Updated :
09 Feb, 2024
Vi Editor, a powerful text editor renowned for its efficiency and versatility, is a staple tool for Unix/Linux users. Mastering its search and replace functionalities can significantly enhance productivity and streamline text editing tasks. In this comprehensive guide, we will delve into various techniques and strategies to effectively search for and replace text within Vi Editor, empowering users to harness its full potential.
Understanding VI Editor
Before delving into search and replace operations, it's crucial to understand the basics of Vi Editor. Developed by Bill Joy in 1976, Vi Editor offers two primary modes: insert mode for inserting text and command mode for executing commands. Familiarity with these modes is essential for efficient navigation and editing within Vi.
There are two modes in the vi editor:
Searching in vi editor
To search for a word in the vi editor follow the below steps:
Step 1: Press Esc if you are in insert mode
Step 2: Press /
Step 3: Type the word or pattern you want to search
Step 4: Press Enter to seach
Step 5: Press 'n' to find the next occurence of word/pattern and 'N' to go to previous occurence
Example: /is

In the previous command you notice that it searches the pattern within a word also like if I'm searching for 'is' then the previous command also include 'distribution' because it contains 'is' in it but, if you want to seach whole word follow the below steps:
Step 1: Press Esc if you are in insert mode
Step 2: Press /
Step 3: Type \< to mark the begining of word
Step 4: Type the word you want to seach
Step 5: Type \> to mark the end of word
Step 6: Press Enter to seach the word
Example: /\<is\<

Replacing in vi editor
To find and replace word in vi editor we use :substitute or :s command syntax of the command is as follows:
:[range]s/{pattern}/{string}/[flags] [count]
The command searches the pattern in [range] lines and replace the [pattern] with [string]. If [range] is not mentioned then command will replace the words in the current line with [string] only.
Example - :s/was/were

You have noticed that the previous command only replace one occurence of the [word] if you want to replace all the occurences of the word add g flag in the command,
Example - :s/was/were/g
To replace all occurences in file Using Vi Editor
To replace all occurences in the file we will use wildcard charcater '%' , just add '%' character before the command to use it,
Example - :%s/was/were/g

Replace the word within the given range in Vi Editor
In the syntax of :s command described earlier in this article we have seen how to add range in the command to replace words only in a given range
:[range]s/[word]/[string]/[flag]
Example - :3,10s/was/were/g in this command the editor will replace the word 'was' wtih 'were' in the lines from 3 to 10 including the extremes.
You can also add wildcard characters in commands mentioned below:
- % (Percentage): Matches all the lines in the file
- . (Dot): Refers to the current line
- $ (Dollar): Denotes the end of the file
Examples:
- :.,$s/was/were/g - Replaces all the occurences of word 'was' with 'were' from current line to the end of file
- :1,.s/was/were/g - Replaces all the occurences of word 'was' with 'were' from starting of file to current line
Example:
In the below screenshot the command :.,$s/was/were/g will replace all the occurences of the word 'was' with 'were' from the current line to the end of file.

Ignore case sensitivity in Vi Editor
To ignore the case sensitivity of the pattern/word use 'i' flag, you can use the flag with any of the command described above.
Example - :1,.s/Was/were/gi

How to Search and Replace in Vi Editor - FAQs
How do I search for a specific word in Vi Editor?
To search for a specific word in Vi Editor, you can use the search command. In Vi, search mode is activated by pressing `/`, followed by the word you want to search for, and then pressing `Enter`. Vi will highlight the first occurrence of the word, and you can navigate through multiple occurrences using `n` for the next occurrence or `N` for the previous one.
/word_to_search
Can I replace multiple instances of a word at once in Vi Editor?
Yes, you can replace multiple instances of a word at once in Vi Editor using the substitute command. In command mode, type `:%s/old_word/new_word/g` and press `Enter`. This command will replace all occurrences of `old_word` with `new_word` throughout the entire file.
:%s/old_word/new_word/g
What are the differences between search and replace modes in Vi Editor?
In Vi Editor, search mode is used to find occurrences of a specific pattern within the text, while replace mode is used to replace occurrences of a pattern with another string. Search mode is activated by pressing `/`, and replace mode is activated by using the substitute command `:%s/old_pattern/new_string/g`.
For search mode:
/pattern_to_search
For replace mode:
:%s/old_pattern/new_string/g
How can I perform a case-sensitive search or replace in Vi Editor?
To perform a case-sensitive search in Vi Editor, use the `/` command followed by `\c` before the search pattern.
For example: To search for `word` in a case-sensitive manner, you would type `/\<word\>/\c` and press `Enter`. For case-sensitive replacement, use the substitute command with the `g` flag: `:%s/old_word/new_word/g`.
For case-sensitive search:
/\<word\>/\c
For case-sensitive replace:
:%s/old_word/new_word/g
Is there a way to undo a search or replace operation in Vi Editor?
Yes, you can undo a search or replace operation in Vi Editor using the `u` command in command mode. After performing a search or replace operation, simply press `u`, and Vi Editor will undo the last change made to the file, reverting it to its previous state.
u
Conclusion
In this article we discussed how to find and change text in Vi Editor which a popular tool in Linux. We started by understanding what Linux and Vi Editor are. Then, we explored how to search for words or phrases in Vi Editor and replace them with new ones. We also looked at some useful tricks like searching for whole words and making case-sensitive changes. Additionally, we covered common questions like how to undo changes. By following these tips, you can become more efficient at editing text in Vi Editor.
Similar Reads
Mastering Search and Replace in Vi Editor
Vi Editor, a powerful text editor renowned for its efficiency and versatility, is a staple tool for Unix/Linux users. Mastering its search and replace functionalities can significantly enhance productivity and streamline text editing tasks. In this comprehensive guide, we will delve into various tec
6 min read
Searching and Replacing in Vim Editor
Vim is an excellent version of the legacy Vi editor for terminals. It is a complete command line-based text editor that has been the go-to choice for IT professionals for decades. Vim is the Vi-IMproved version of the original text editor which contains many modern features. In this article, we shal
7 min read
How to search and replace text in a file in Python ?
In this article, we will learn how we can replace text in a file using python. Method 1: Searching and replacing text without using any external module Let see how we can search and replace text in a text file. First, we create a text file in which we want to search and replace text. Let this file b
5 min read
Undo and Redo in Vi editor
Vi is a versatile text editor that's available for Unix and Linux systems. It's packed with powerful features, one of which is the ability to undo and redo changes you make to a file. This means if you accidentally delete something or make a mistake, you can easily go back and fix it. Understanding
6 min read
How to Use Find and Replace in Google Docs
Do you ever find yourself spending too much time manually searching and updating repetitive words or phrases in your Google Docs? Whether you're working on a long report, an essay, or any other document, the "Find and Replace in Google Docs" feature can be a real lifesaver. This handy tool allows yo
6 min read
How to Search in Nano Editor?
The Nano editor is a command-line text editor widely used in Unix-like operating systems, including Linux and macOS. Despite its simplicity, it offers a powerful search functionality that allows users to quickly locate and navigate to specific text within a file. Mastering the search feature in Nano
6 min read
Macros in Vi editor
Macros in Vi Editor are particularly handy for automating repetitive edits, such as commenting or uncommenting lines, transforming text, or making similar changes across multiple lines or files. They add a layer of efficiency to the text editing process and are a valuable tool for users who want to
7 min read
How to Find and Replace Text in MS Word?
MS-Word is a powerful word processing application, used to create documents, articles, assignments, etc. with the help of features and tools provided by it. It is used to perform many operations and creating or editing files and documents. MS Word is popular in the market because of its wide variety
3 min read
Edit Multiple Lines in Vi edtor
In this article, we will cover how to edit multiple lines in vi editor. First, we look at what Linux and VI editors followed by editing multiple lines in the vi editor with examples and then we will look how to delete multiple lines in the vi editor. LinuxLinux is a kernel for operating system(OS) i
4 min read
Joining Two Lines in VI Editor
Vi is a powerful command-line text editor used by programmers, system administrators, and writers alike. They offer a vast array of features, including the ability to manipulate text efficiently. One common task you might encounter when working with Vi is joining two lines together. This article wil
3 min read