About Linux FAQ

Browse More FAQs:

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

Posted by Vivek Gite [Last updated: May 12, 2008]

This FAQ is part 4 of 9 in the series VI / VIM Text Editor

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.

Navigation for VI / VIM Text Editor«Ubuntu Linux Vim Sorry, the command is not available in this version: syntax onFreeBSD install VIM text editor»

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. Fletch Says:

    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!

  2. yoander Says:

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

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.