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?

Advertisement

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.

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

19 comments… add one
  • Shawn Welch Jun 30, 2011 @ 14:05

    Thanks so much. Vim is great!

  • Krunal Aug 12, 2011 @ 7:39

    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 Aug 12, 2011 @ 7:42

    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 Feb 20, 2012 @ 14:05

    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 Oct 24, 2012 @ 13:34

    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 Nov 7, 2012 @ 22:36

    :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 @ 22:28

    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

  • JAYESH Sep 26, 2015 @ 15:17

    if i just want replace only first 10 occurance of a word in a text file then what will be the vim command.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.