Vi / VIM Find And Replace All Text Substitute Command

last updated in Categories , ,

I am a new Linux user. I started to use vim or vi text editor. How do I find all occurrence of the word called ‘eth0’ and replace it with ‘br0’ on Linux operating systems?

Both vi and vim text editor provides the substitute command. It is used for find/search and replace text. This page explains how to use substitute command for finding and replacing text with vim/vi.

VIM Search and replace syntax

The syntax is as follows:
## Search and replace in the current line only ##
## Change each 'Unix' to 'Linux' for all lines from line 36 to line 42 ##

VIM Find And Replace

Both vi and vim provides the :s command for search and replace. Let us see some easy to understand examples.


I am going to use the following sample text file:


## full acess to lo and eth0 ##
-A INPUT -i lo -j ACCEPT
-A INPUT -i eth0 -j ACCEPT
-A OUTPUT -o eth0 -j ACCEPT

# log spoof
-A INPUT -i eth1 -s -j LOG --log-prefix "IP DROP SPOOF A: "
-A INPUT -i eth1 -s -j DROP

-A INPUT -i eth1 -s -j LOG --log-prefix "IP DROP SPOOF B: "
-A INPUT -i eth1 -s -j DROP

To find each occurrence of ‘eth0’ in the current line only, and replace it with ‘br0’, enter (first press Esc key and type):

Next try to find and replace all occurrences of ‘eth1’ with ‘br1’ under vim, enter:

Search and replace all occurrences of ‘eth1’ with ‘br1’, but ask for confirmation first on vim, enter:

To find and replace all occurrences of case insensitive ‘eth1’ with ‘br1’, enter:
The above example will find eth1, ETH1, eTh1, ETh1 and so on and replace with br1. To find and replace all occurrences of ‘eth1’ with ‘br1’ for lines from 3 to 7, enter:

A quick demo

Animated gif 01: Vim Find and Replace Demo
Animated gif 01: Vim Find and Replace Demo

Substitute syntax

The full syntax is as follows:
:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
So for each line in [range] replace a match of {pattern} with {string}. A {pattern} is one or more branches. A {string} can be a literal string. When [range] and [count] are omitted, replace in the current line only. When [count] is given, replace in [count] lines, starting with the last line in [range]. When [range] is omitted start in the current line. The [count] must be a positive number.

On each line, delete all occurrences of the whole word “Sysadmin”

Yes, we can delete words too:

How can I delete the first occurrence of the whole word “method_” and the following 3 characters?



You learned how to find and replace text with vi or vim text editor using the %s substitute command. Type the following command inside vim to get help about substitute topic or online here:
:help :substitute


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at

Historical Comment Archive

7 comment

  1. what about skipping characters?
    I have ../aaa/ and I want to replace it with bbb/
    I tried the following but it didn’t work:
    :%s /..\/aaa\//bbb\//gc

  2. If one line contain two identical words and i want to replace only one word which i want,
    Which command is use?

    Still, have a question? Get help on our forum!