Home > HowTo, Linux > How to find and replace text using vi

How to find and replace text using vi

April 17th, 2013 Leave a comment Go to comments

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

Categories: HowTo, Linux Tags: , , , , ,
  1. No comments yet.
  1. No trackbacks yet.

VAMOVE *

*