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


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:

Share this tutorial on:

Like this? Follow us on Twitter OR support us by using Patreon

{ 36 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

    good tutor. thank you



  • 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


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


  • 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

  • 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

  • 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


  • Max August 27, 2014, 6:04 am

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


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


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

  • madhukar July 9, 2015, 6:19 am

    how to replace the string if there is ‘\’ in word to find and replace word

  • rajan July 17, 2015, 5:40 am

    Thank u very much.it really simple and helpful

  • vaibhav July 28, 2015, 6:36 am

    Is there any support of special character like “*”.
    Suppose I want to replace anything that comes after my search, till “;” (semicolon).
    Example : protocol:http;
    protocol: dhcp;
    There are many parameters the “Protocol” keyword can take.
    Appreciate your help. ( I don’t want to repeat the same replace commands for the different parameters)

  • Ken Patrick Du February 27, 2016, 5:41 am

    Hi guys

    Can some one help me ?
    Problem: All the word that starts with s must be lowercase

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , , , , , , , , , ,