vi Find And Replace Text Command

last updated in Categories , ,

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, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

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. Thanks a lot, but does it mean :100,250s/UNIX/Linux/g instead of :100,200s/UNIX/Linux/g for lines 100-250?

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

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

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

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


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


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

  9. Suppose I have a text file which has list of files with absolute path.
    Now I want to replace /home/username/directory/file with ~/directory/file, How can I do that?

    Still, have a question? Get help on our forum!