vi Find And Replace Text Command

by on November 13, 2010 · 10 comments· last updated at November 17, 2010

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.

Syntax

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/\<UNIX\>/Linux/gc

Task: Case Insensitive Find and Replace

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

Task: Case sensitive Find and Replace

Find each 'UNIX' and replace with 'bar':
:%s/UNIX/bar/gI
Same command with confirmation:
:%s/UNIX/bar/gIc

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):
:s/UNIX/Linux/g
NOTE: You need to prefix % the substitute command to make changes on all lines:
:%s/UNIX/Linux/g

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

:{START-n},{END-n}s/word1/word2/g
Find 'UNIX' and replace with 'Linux' all lines between line 100 and line 250, enter:
:100,200s/UNIX/Linux/g
OR
:100,200s/UNIX/Linux/gc



You should follow me on twitter here or grab rss feed to keep track of new changes.

Featured Articles:

{ 10 comments… read them below or add one }

1 nidirondel November 17, 2010 at 11:49 am

Thanks for the tips, I didn’t knew the “confirmation”.

The section “Find and Replace Whole Word Only” seems incorrect
Find whole words exactly matching ‘UNIX’ to ‘Linux’; and ask for confirmation too:
:%s/\/Linux/gc ?????

Reply

2 Vivek Gite November 17, 2010 at 2:20 pm

Thanks for the heads up!

Reply

3 toto February 25, 2011 at 6:23 am

hi,
good tutor. thank you

regards,

toto

Reply

4 Manvenson July 17, 2012 at 7:37 am

Thanks for the tutor.
It is very helpful.

Reply

5 kArThk October 23, 2012 at 7:51 am

That was really awesome.

Thanks a lot.

Reply

6 riash November 24, 2012 at 6:19 pm

Thanks

Reply

7 yo yo March 6, 2013 at 4:34 am

Nice one dude..thanks

Reply

8 Sanchit April 22, 2013 at 3:26 pm

Nice article. Thanks for sharing your knowlodeg

Reply

9 dhawal May 7, 2013 at 6:39 am

thanks for the help dude

Reply

10 saman May 18, 2013 at 5:52 am

how i find / and replace it.

thank.

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Tagged as: , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: