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.
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 techniques and strategies to effectively search for and replace text within Vi Editor, empowering users to harness its full potential.
Table of Content
- Understanding VI Editor
- Searching in vi editor
- Replacing in vi editor
- To replace all occurences in file Using Vi Editor
- Replace the word within the given range in Vi Editor
- Ignore case sensitivity in Vi Editor
- How to Search and Replace in Vi Editor – FAQs
Contact Us