Open In App

Mastering Search and Replace in Vi Editor

Last Updated : 09 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

Screenshot-from-2023-09-27-19-16-27

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\<

Screenshot-from-2023-09-27-19-29-02

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

Screenshot-from-2023-09-27-19-52-13

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

Screenshot-from-2023-09-27-20-00-12

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.

Screenshot-from-2023-09-27-20-06-45

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

Screenshot-from-2023-09-27-20-17-09

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 Color Theory and Psychology: Advanced Insights for Design
The study of using colors to convey an emotion, create a visual effect, or change human perception is known as Color Theory. Color theory is all about using colors to convey the right message. But how can we use color theory to our advantage? And how does color theory correlate with Psychology? In this article, we will discuss what color theory is,
6 min read
SAP ABAP | Mastering String Manipulation
String manipulation is a fundamental skill in SAP ABAP programming language, and it's crucial for dealing with various types of data processing, text formatting, and building dynamic content. This article aims to guide you through mastering string manipulation in SAP ABAP, providing insights, best practices, and code examples. Table of Content Intr
7 min read
Mastering JWT authentication in Express
While creating any application it is very important to add authentication to ensure that only authorized users can access the protected resources. A JSON Web Token (JWT) is a JSON object utilized to securely transmit information between two parties over the web. Primarily employed in authentication systems, JWTs can also facilitate secure data exch
3 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 can significantly improve productivity and effici
6 min read
Difference between vi Editor and cat Command
Both vi editor and cat command are used to input contents in a file. 1. vi Editor : vi editor stands for the visual editor which is an intelligent editing tool in UNIX Operating System used for reading, creating files, editing them, and more. It is the default editing tool in UNIX. Command to open a particular file : vi filename It has three modes
3 min read
Nano vs VIM editor – What’s the difference between nano and vim editors?
In this article, we will understand the difference between nano and vim editors in Unix/Linux. Before diving into this topic, we will first understand what exactly nano and vim editor is. Nano Editor in the Linux System. Nano is a simple, user-friendly text editor that improves user experience. Its GUI (Graphical User Interface) makes it easy to us
3 min read
How to Comment and Uncomment multiple line vi terminal editor
IntroductionThe Vi and Vim text editors are renowned for their efficiency in handling various text-editing tasks. One such task that often comes up in programming and text manipulation is the need to comment or uncomment multiple lines of code or text. Whether you're annotating sections of your code for clarity or removing comments to activate spec
2 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 shall explore one such feature, the functionality to s
7 min read
How to make Live Coding Editor using HTML CSS and JavaScript ?
In this article, we will implement a live coding editor using HTML, CSS, and JavaScript. This project will allow you to write and execute HTML, CSS, and JavaScript code in real-time, making it an excellent tool for learning and testing your code snippets. Final Output PrerequisiteHTMLCSSJavaScriptApproachBegin by defining the HTML, CSS, and JavaScr
3 min read
How to Save and Exit in Nano Editor in linux
Saving and exiting in the Nano text editor in Linux is a fundamental skill for anyone working with text files in the terminal. Nano is a user-friendly and straightforward text editor, making it an excellent choice for both beginners and experienced Linux users. Whether you're editing configuration files, writing scripts, or taking notes, knowing ho
5 min read