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.

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

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

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

    Thanks
    Ashok

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

  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:dns;
    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)
    Thanks,
    Vaibhav

  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?

Comments are closed.