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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads