≡ Menu

UNIX / Linux: vi / vim perform search and replace operation

Q. I’ve just installed CentOS Linux server and started to use vi text editor to make changes to config files. How do I perform search and replace operation using vi / vim text editor?

A. vi (vim) is not difficult to learn, the vi editor is well known and used for both writing code and editing config files.

VI search and replace command format

Simple format is as follows:
%s/old-string/new-string/

VI search and replace command examples

Let us say you would like to find a word called "foo" and replace with "bar".

First hit [Esc] key

Type : (colon) followed by %s/foo/bar/ and hit [Enter] key.
:%s/foo/bar/
Above command will replace first occurrence of word foo with bar on all lines. The % is shorthand for all lines.

To replace all occurrences of word foo with bar on all lines, use the g option (which indicates all occurrences on a line).
:%s/foo/bar/g

Note that the g can be replaced with a number 1,2,...N to change only the n'th occurrence on each line.

Use find and replace on line ranges (match by line numbers)

You can also make changes on range of lines i.e. replace first occurrence of foo with bar on lines 5 through 20 only, enter:
:5,20s/foo/bar/

Following command will replace first occurrence of foo with bar starting at the current line for the next 100 lines:
:.,+100s/foo/bar/

Match by words

Finally, you can match by words i.e. replace first occurrence of foo with bar starting at at the next line containing a word "test":
:/test/s/foo/bar/g
As usual you can specify ranges:
:/test/,/guest/s/foo/bar/g

Please note that all search/replace commands should be start with the [ESC]: keystroke combination only.

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

{ 18 comments… add one }

  • Fletch August 11, 2007, 9:43 am

    Hi,

    I’m fairly new to vi and I’ve recently been making bulk changes on config files in vi using the following example;

    :1,$s/sometexttoreplace/newtext

    The $s means to the end of the file. I also found you need to escape characters like . with \.

    However, I didn’t realise you could use the /g option to limit change to one line – that’s really helped me a lot!

  • yoander May 12, 2008, 4:14 pm

    You could also use regular expression. Suppose you have the httpd.conf
    in this way:

    <VirtualHost www.mydomain1.com>
    ...
    </VirtualHost>
    <VirtualHost www.mydomain2.com>
    ...
    </VirtualHost>
    <VirtualHost www.mydomainn.com>
    ...
    </VirtualHost>
    and you want them in this another way:
    <VirtualHost IP:Port>
    ...
    </VirtualHost>
    

    simply do:

    vim /etc/httpd/conf/httpd.conf

    inside vim editor en command mode write:

    1,$s/<VirtualHost.*>/<VirtualHost IP:80>/
  • markus July 18, 2008, 12:56 pm

    How to apply this on multiple files?

  • yj July 22, 2008, 10:44 am

    How can I find and replace 2 different words from one paragraph using one vi command..?

  • JOsh Beauregard March 5, 2009, 6:43 pm

    thanx for the good article, as I have learned vim I have returned here a couple of times for different search methods.

  • BISHNU April 13, 2009, 7:03 am

    I WANT TO KNOW THE SEARCH OPTION LIKE IN WINDOWS THAT SOME WORD WE DON’T CHANGE AND SOME WE CHANGE,IS THERE ANY OTHER METHOD RATHER THIS
    :5,20S/FOO/BAR

    THIS IS THE METHOSD TO CHANGE A LIMITED WORDS BUT CAN I DO ;LIKE
    :5,20,25,35S/FOO/BAR

  • BISHNU April 13, 2009, 7:04 am

    I WANT TO KNOW THE SEARCH OPTION LIKE IN WINDOWS THAT SOME WORD WE DON’T CHANGE AND SOME WE CHANGE,IS THERE ANY OTHER METHOD RATHER THIS

  • Umesh May 20, 2009, 12:01 am

    The article is really helpful for the people who want to learn REPLACE command from vi editor.

  • Kafulu Zed August 28, 2009, 9:52 am

    Thanks for this, I’m beginning to sink my teeth into Linux and this is another one of those commands that has saved me half an hour’s editting in my first few attempts. I will definately spread the “GOSPEL”

  • Surendran April 23, 2011, 9:29 am

    I want to add word ‘cp ‘ in each line of my file test.001 which has around 400 lines. how can i do this thru vi editir? In the same way, how can a add ward at the end of each line ?
    Your help is highly appreciated. I can do this thru cat and awk commands. But I want to know haw can I do this in vi editor.

    Thanks

    • James May 13, 2011, 9:02 pm

      I know this is old, but I hate to see unanswered questions.

      For the beginning:
      :%s/^/cp /

      For the end:

      :%s/$/ BlahBlahBlah/

      For the beginning and end:

      :%s/^\(.*\)$/BeginningWord \1 EndWord/

  • Shawn Welch June 30, 2011, 2:05 pm

    Thanks so much. Vim is great!

  • Krunal August 12, 2011, 7:39 am

    i want o find a pattern and replace it
    for ex:- line 1 1234,9876540
    line 2 56789000,987654012

    In above example i want to replace 1234, and 56789000, with
    So the output should be
    line 1 9876540
    line 2 987654012
    Basically i want to find the pattern that stars with and ends with , and replace it with

    Please help

  • Krunal August 12, 2011, 7:42 am

    i want o find a pattern and replace it
    for ex:- line 1 pnonenumber1234,9876540pnonenumberq
    line 2 pnonenumber56789000,987654012

    In above example i want to replace pnonenumber1234, and pnonenumber56789000, with pnonenumber.

    So the output should be
    line 1 pnonenumber9876540pnonenumberq
    line 2 pnonenumber987654012pnonenumberq
    Basically i want to find the pattern that stars with pnonenumber and ends with , and replace it with

    Please help

  • sameer gupta February 20, 2012, 2:05 pm

    hello sir,
    I have shared server, all maximum php file is infected with the following line of code
    I want to replace following all lines with single word or blnk., Please tell me if there is an any linux script to remove following lines

  • Herb Miller October 24, 2012, 1:34 pm

    I would like to replace the last occurence of a pattern in all lines (or even just 1 line might help). In lines below, I want to change only the last occurence of foo to bar.
    change fooaaaaafooaaaabbbfooxxx to fooaaaaafooaaaabbbbarxxx
    change aaafoobbbbfoocccfoozz to aaafoobbbbfoocccbarzz
    change fooaaaafoobbbbfoo to fooaaaafoobbbbbar
    change aaafoobbbfoocccfoo to aaafoobbbfoocccbar
    Thanks.

  • Jagan saha November 7, 2012, 10:36 pm

    :1,$s/sometexttoreplace/newtext
    example:
    :1, $s/a2b/a3bc/
    works fine to replace the first occurance in each line of entire file, i want to replce second occurance and/or nth occurance – please help me with command. thanks in advance for time &help.

    Thanks,
    -Jagan

  • Nathan May 2, 2013, 10:28 pm

    Good afternoon,
    In a VI ex command how many strings can there be?
    for example, if i use this set of strings, how many strings can i have in one command?
    :%s/\(.*\)\/\(.*\)\/.*\/\(.*\)/\1 \2 \3 \4

Leave a Comment