How to find and replace text using vi or vim
Both vi and vim text editor comes with substitute command for finding and replacing text.
The syntax is as follows:
:%s/WORD-To-Find-HERE/Replace-Word-Here/g
OR
:%s/FindMe/ReplaceME/g
Examples
The substitute command can be used as per your requirements.
Task: VI / Vim Basic Find and Replace
To find each occurrence of ‘UNIX’, and replace it with ‘Linux’, enter (press ESC, type : and following command):
:%s/UNIX/Linux/g
Task: Find and Replace with Confirmation
Find a word called ‘UNIX’ and replace with ‘Linux’, but ask for confirmation first, enter:
:%s/UNIX/Linux/gc
Task: Find and Replace Whole Word Only
Find whole words exactly matching ‘UNIX’ to ‘Linux’; and ask for confirmation too:
:%s/\/Linux/gc

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

*