LKBEN11101: Howto make search and replaces in vim


Symptom

You need to search and replace in a vim document

Cause

Vim is extremely good in these things although it is not that simple

Solution

To search and replace you can use the "substitute" command. This can be done by writing :substitute or with the short version :s. The command needs to be followed by a text to be searched, a

text for the replacement and some extra information.

Suffixes for the command:

g: global — each occurrence in the line is changed, not just the first one of every line!
i: case insensitive
I: case sensitive
c: confirmation - ask me first

The command also can have a prefix:

%: applies to every line, not just the current line
a,b: where a and b are numbers. From line a till line b
'a,'b: where a and b are numbers. All lines between the a and b marks
.,$: between the current line (.) and the last line ($)
.,+5: between the current line (.) and the next 5 lines (+5)
1,$: between the first line and the last line (hey, this is the same as %)


There are a lot of options, but these are the most needed ones:

The following will find each occurrence of "foo" in your text, and replace it with "bar".

:%s/foo/bar/g

or

:%substitute/foo/bar/g

You can use the same command and add confirmation.

:%s/foo/bar/gc

If your want to change whole words only (exact matching of foo)

:%s/\<foo\>/bar/gc

To be case insensitive, you can add an i at the end.

:%s/foo/bar/gci

The previous command changes "foo" to "bar" globally, case insensitive and ask for confirmation.

Of course we can ask to be case sensitive by adding a capital I at the end.

:%s/foo/bar/gcI


Some more information about what you are searching:

    \/ is / You need to escape the forward slash with a backslash
    \t is tab, \s is any whitespace
    \n is newline, \r is CR (carriage return = Ctrl-M = ^M)

When replacing:

    \r is newline, \n is a null byte (0x00).
    \& is ampersand which needs to be escaped too (& is the search pattern).

You can use other delimiters with substitute which can be usefull for URL's

    :s#http://www.example.com/index.html#http://example.com/#

In this case you can avoid escaping the / character.

For mor information look at the help which is included in vim.

Nice to know is the fact that a Ctrl-z can undo all the changed you just made!

Disclaimer:

The information provided in this document is intended for your information only. Lubby makes no claims to the validity of this information. Use of this information is at own risk!

About the Author

Author: Wim Peeters - Keskon GmbH & Co. KG

Wim Peeters is electronics engineer with an additional master in IT and over 30 years of experience, including time spent in support, development, consulting, training and database administration. Wim has worked with SQL Server since version 6.5. He has developed in C/C++, Java and C# on Windows and Linux. He writes knowledge base articles to solve IT problems and publishes them on the Lubby Knowledge Platform.