Monday, January 14, 2013

VI Find And Replace Text Commands with examples

How do I find and replace (substitute) test using vi or vim text editor under UNIX / Linux / BSD or Apple OS X operating systems?

Both vi and vim text editor comes with substitute command for finding and replacing text.


The syntax is as follows:


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):

Task: Find and Replace with Confirmation

Find a word called 'UNIX' and replace with 'Linux', but ask for confirmation first, enter:

Task: Find and Replace Whole Word Only

Find whole words exactly matching 'UNIX' to 'Linux'; and ask for confirmation too:

Task: Case Insensitive Find and Replace

Find 'UNIX' (match UNIX, unix, UnIx, Unix and so on) and replace with 'Linux':
Same command with confirmation:

Task: Case sensitive Find and Replace

Find each 'UNIX' and replace with 'bar':
Same command with confirmation:

How Do I Replace In the Current Line Only?

Find 'UNIX' and replace with 'Linux' in the current line only (note % is removed from substitute command):
NOTE: You need to prefix % the substitute command to make changes on all lines:

How Do I Replace All Lines Between line 100 and line 250?

Find 'UNIX' and replace with 'Linux' all lines between line 100 and line 250, enter:

No comments:

Post a Comment