Search and replace with Vim and Sed

Using search and replace is a great way to save time when editing large files in Linx. Becoming proficient with this task will increase your efficiency and will reduce your time spent doing tedious and error-prone file edits by hand.

For the sake of this tutorial, I’m going to use a copy of the /etc/apt/sources.list file to illustrate some of the changes that we can make. From your users home directory copy the sources.list file to your home directory. Or you can copy the contents of my listed below into a new file.

This tutorial assumes that you are comfortable reading and writing files with vim. If not open a terminal and type:  vimtutor

sudo cp /etc/apt/sources.list /home/luke/sources.txt
sudo chown $USER:$USER sources.txt

Here are the contents of my sources.list file.

cat sources.txt
# See for how to upgrade to
# newer versions of the distribution.
deb xenial main restricted
deb-src xenial main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb xenial-updates main restricted
deb-src xenial-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb xenial universe
deb-src xenial universe
deb xenial-updates universe
deb-src xenial-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb xenial multiverse
deb-src xenial multiverse
deb xenial-updates multiverse
deb-src xenial-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb xenial-backports main restricted universe multiverse
deb-src xenial-backports main restricted universe multiverse

deb xenial-security main restricted
deb-src xenial-security main restricted
deb xenial-security universe
deb-src xenial-security universe
deb xenial-security multiverse
deb-src xenial-security multiverse


In vim, you can use the :substitute command, which is pretty much always abbreviated as just  :s  followed by a pattern that is separated by forward slashes like this.  :s/find/replace/ . This command tells vim to search the current line for “find” and replace it with with “replace”.

To perform a search of all lines change  :s to  :%s . The % in front of “s” tells Vim that we want to search every line and will replace the first instance of our search term with our replace term as this example will show.

vim sources.txt

Pressing enter will show give you a summary of how many changes are being made. You can review the file for accuracy and then save the file or quit without saving. Notice that every line is searched.

Vim global search and replace

To change every instance of a word and not just the first instance on a line we need to add the global option to our command.

vim sources.txt
:%s/ubuntu/OLD YELLER/g

Here you can see that every instance of “ubuntu” has been changed to “OLD YELLER”.

If you wanted to find and delete a word you can use the format  %s/<search term>// leaving the replace field empty. For example to remove comment’s from the file.


Sed – Stream Editor

The command syntax for  sed actually isn’t much different from vim. By default  sed will output to standard output and will not make changes to a file unless you specify options to allow it to save changes.

Similar to our vim example to change every occurrence of “xenial” to “yakkety”:

sed 's/xenial/yakkety/' sources.txt

 sed global edit

As with vim, you can use the “g” option to make global changes. By default  sed will only change the first instance of a search term unless you append g to the end of the search line.

sed 's/ubuntu/OLD YELLER/g' sources.txt

Deleting words from a file works the same way as vim as well.

sed 's/ubuntu//g' sources.txt


sed with in-place editing

You can make changes to files with  sed-i . Using the -i option will create a backup copy of the original file before making edits, which will save you from inadvertently ruining a working configuration.

sed -i.bak 's/xenial/yakkety/' sources.txt

Learning to use search and replace in sed and vim will make your life as a Linux Administrator far easier and is well worth the time you will spend becoming comfortable with using these tools.

Leave a Reply