vi Find And Replace Text Command

by on November 13, 2010 · 25 comments· LAST UPDATED November 17, 2010

in , ,

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

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 25 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 nixCraft 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 ritesh May 2, 2013 at 4:44 am

very helpful

Reply

10 dhawal May 7, 2013 at 6:39 am

thanks for the help dude

Reply

11 saman May 18, 2013 at 5:52 am

how i find / and replace it.

thank.

Reply

12 ashok sharma May 27, 2013 at 3:23 am

great guidens

Reply

13 Madhav June 14, 2013 at 11:17 am

please let me know how to replace W* with abc

Reply

14 Pibomb July 5, 2013 at 2:41 am

Thanks a lot, but does it mean :100,250s/UNIX/Linux/g instead of :100,200s/UNIX/Linux/g for lines 100-250?

Reply

15 vivek tudi July 24, 2013 at 9:40 am

its good documentation

Reply

16 vivek tudi July 24, 2013 at 9:41 am

it is easy to understand

Reply

17 rajesh August 5, 2013 at 8:49 am

Thanks for sharing.. Its good and quick

Reply

18 ibraheien August 20, 2013 at 7:54 pm

Thansk ,it is help me very much …
but how to search & replace if the keyword contain / char ?
for example (life example)
I try to edit xml file and i want to change every with ?
thanks ….

Reply

19 Amit November 13, 2013 at 3:31 pm

Hi All,

This is really nice stuff.
But I want to replace all occurrence of ‘./’ in my file in vi editor with /application/tf3.
first_string= ./
new_string= /application/tf3
can anyone please help

Reply

20 sumit singh December 2, 2013 at 5:15 am

Thanks for the tips, these proved very useful for me.

Reply

21 vivek January 13, 2014 at 9:23 am

Hi Vivek,

How to replace C:\\Projects\\website\\upload to /home/httpd/www

Reply

22 Niranjan K January 21, 2014 at 3:09 pm

Thank you……always helping!!!! Nice site….!!!

Reply

23 nayeem February 12, 2014 at 1:33 pm

i need to replace ’04/05/3013′ date with ’04/10/2013′ througout the file.
please help and let me know the exact command.

Reply

24 tomytree22 April 8, 2014 at 7:20 pm

You must escape character / with \
then replace it
04/05/3013 by 04\/05\/3013

Reply

25 Ashok July 28, 2014 at 1:04 pm

Any one help me how to replace the string within multiple lines which choose randomly

Suppose my file text.txt contain 100 lines , every line also have same string name “Hello”, But my requirement is I have to replace the String name “Hello” to “Hi”
in line no ,1,10, 28, 45.
if I am using 1,10, 28, 45s/Hello/Hi —Then all the lines from 1 to 45
Pls Let me know how to replace the string within that specific lines only

Thanks
Ashok

Reply

Leave a Comment

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

Previous Faq:

Next Faq: