vi Find And Replace Text Command

Posted on in Categories , , last updated 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.


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:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 37 comments/add one below):

37 comment

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

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

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


  5. 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)


  6. 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)

Leave a Comment