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.


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:

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 ?????


2 nixCraft November 17, 2010 at 2:20 pm

Thanks for the heads up!


3 toto February 25, 2011 at 6:23 am

good tutor. thank you




4 Manvenson July 17, 2012 at 7:37 am

Thanks for the tutor.
It is very helpful.


5 kArThk October 23, 2012 at 7:51 am

That was really awesome.

Thanks a lot.


6 riash November 24, 2012 at 6:19 pm



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

Nice one dude..thanks


8 Sanchit April 22, 2013 at 3:26 pm

Nice article. Thanks for sharing your knowlodeg


9 ritesh May 2, 2013 at 4:44 am

very helpful


10 dhawal May 7, 2013 at 6:39 am

thanks for the help dude


11 saman May 18, 2013 at 5:52 am

how i find / and replace it.



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

great guidens


13 Madhav June 14, 2013 at 11:17 am

please let me know how to replace W* with abc


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?


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

its good documentation


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

it is easy to understand


17 rajesh August 5, 2013 at 8:49 am

Thanks for sharing.. Its good and quick


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 ….


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


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

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


21 vivek January 13, 2014 at 9:23 am

Hi Vivek,

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


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

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


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.


24 tomytree22 April 8, 2014 at 7:20 pm

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


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



Leave a Comment

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

Previous Faq:

Next Faq: