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

by Vivek Gite on July 30, 2007 · 14 comments

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.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 14 comments… read them below or add one }

1 Fletch August 11, 2007

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!

Reply

2 yoander May 12, 2008

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

<VirtualHost http://www.mydomain1.com>
...
</VirtualHost>
<VirtualHost http://www.mydomain2.com>
...
</VirtualHost>
<VirtualHost http://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>/

Reply

3 markus July 18, 2008

How to apply this on multiple files?

Reply

4 yj July 22, 2008

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

Reply

5 JOsh Beauregard March 5, 2009

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

Reply

6 BISHNU April 13, 2009

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

Reply

7 BISHNU April 13, 2009

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

Reply

8 Umesh May 20, 2009

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

Reply

9 Kafulu Zed August 28, 2009

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”

Reply

10 Surendran April 23, 2011

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

Reply

11 James May 13, 2011

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/

Reply

12 Shawn Welch June 30, 2011

Thanks so much. Vim is great!

Reply

13 Krunal August 12, 2011

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

Reply

14 Krunal August 12, 2011

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

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 9 + 10 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: