≡ Menu

vi Find And Replace Text Command

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

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 32 comments… add one }

  • nidirondel November 17, 2010, 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 ?????

  • nixCraft November 17, 2010, 2:20 pm

    Thanks for the heads up!

  • toto February 25, 2011, 6:23 am

    hi,
    good tutor. thank you

    regards,

    toto

  • Manvenson July 17, 2012, 7:37 am

    Thanks for the tutor.
    It is very helpful.

  • kArThk October 23, 2012, 7:51 am

    That was really awesome.

    Thanks a lot.

  • riash November 24, 2012, 6:19 pm

    Thanks

  • yo yo March 6, 2013, 4:34 am

    Nice one dude..thanks

  • Sanchit April 22, 2013, 3:26 pm

    Nice article. Thanks for sharing your knowlodeg

  • ritesh May 2, 2013, 4:44 am

    very helpful

  • dhawal May 7, 2013, 6:39 am

    thanks for the help dude

  • saman May 18, 2013, 5:52 am

    how i find / and replace it.

    thank.

  • ashok sharma May 27, 2013, 3:23 am

    great guidens

  • Madhav June 14, 2013, 11:17 am

    please let me know how to replace W* with abc

  • Pibomb July 5, 2013, 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?

  • vivek tudi July 24, 2013, 9:40 am

    its good documentation

  • vivek tudi July 24, 2013, 9:41 am

    it is easy to understand

  • rajesh August 5, 2013, 8:49 am

    Thanks for sharing.. Its good and quick

  • ibraheien August 20, 2013, 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 ….

  • Amit November 13, 2013, 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

    • Ravindra Naik May 1, 2015, 4:10 am

      Try this
      :%s#/#/application/tf3#g

  • sumit singh December 2, 2013, 5:15 am

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

  • vivek January 13, 2014, 9:23 am

    Hi Vivek,

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

    • Ravindra Naik May 1, 2015, 4:09 am

      Try this
      :%s#C:\\Projects\\website\\upload#/home/httpd/www#g

  • Niranjan K January 21, 2014, 3:09 pm

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

  • nayeem February 12, 2014, 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.

    • tomytree22 April 8, 2014, 7:20 pm

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

  • Ashok July 28, 2014, 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

  • Max August 27, 2014, 6:04 am

    If you change /home/httpd to /var/httpd in a textfile, using \ before / is handy:

    :s/var\/http/\/home/httpd/g

    (In case people are wondering why not just use var and home, I can tell you that var is represented elsewhere in the file I edited)

    Sincerely
    Max

  • vaibhav atalkar December 30, 2014, 10:39 am

    thank you very much ….

  • chris January 5, 2015, 9:41 am

    sorry, but “…/gc” is only vim-style, isn’t it?

  • Vikas mahajan April 3, 2015, 1:30 pm

    Thanks to all this help

  • saurabh Tandon April 13, 2015, 6:16 am

    Nice …. It helped me..

Leave a Comment