sponsor Vim development Vim logo Vim Book Ad

basic Tip #31: Find and Replace

 tip karma   Rating 1997/778, Viewed by 112747 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   March 7, 2001 5:49      complexity:   basic
author:   Anonymous      as of Vim:   5.7

To find and replace one or more occurences of a given text pattern with a
new text string, use the s[ubstitute] command.

There are a variety of options, but these are what you most probably want:

:%s/foo/bar/g           find each occurance of 'foo' and replace it with 'bar' without asking for confirmation

:%s/foo/bar/gc          find each occurance of 'foo' and replace it with 'bar' asking for confirmation first

:%s/<foo>/bar/gc      find (match exact word only) and replace each occurance of 'foo' with 'bar'

:%s/foo/bar/gci         find (case insensitive) and replace each occurance of 'foo' with 'bar'

:%s/foo/bar/gcI         find (case sensitive) and replace each occurance of 'foo' with 'bar'


NB: Without the 'g' flag, replacement occurs only for the first occurrence in each line.

For a full description and some more interesting examples of the substitute command refer to

:help substitute

See also:

:help cmdline-ranges
:help pattern
:help gdefault

 rate this tip  Life Changing Helpful Unfulfilling 

<< Increasing or decreasing numbers | Write your own vim function(scripts) >>

Additional Notes

[email protected], May 30, 2001 1:57
To match exact words only you need \<foo\>, not <foo>.
Anonymous, November 25, 2002 5:22
To find or replace line breaks or tab characters, use Ctrl-v followed by the enter or tab key.
[email protected] - NOSPAM, October 12, 2004 13:24
As an alternative to embedding control characters into your searches and substitutes:

tabs ~ \t
newline ~ \n  when searching or on the left-hand-side of a substitute
newline ~ \r  when inserting a newline with a substitute (right-hand-side)
Anonymous, February 15, 2005 11:59
VIMs find and replace also supports wild carding with '*'
[email protected], May 18, 2005 15:26
How do I find and replace all commas with a new line?

:%s/,/\n/gc   # places control characters in, as does
:%s/,/~ \n/gc  

How do you insert the actual newline?
Anonymous, May 26, 2005 4:27
to bwiese: try :s%/,/\r/gc instead...
Anonymous, June 17, 2005 23:32
to bwise

you have to type C-v C-n to get the actual newline.
[email protected], July 22, 2005 4:48
thanks! that did it: ":%s/,/\r/gc" and also using "Ctrl-v Ctrl-m" to get the "^M" for the replace string in ":%s/,/^M/gc"  (posted to correct minor typos)
[email protected], October 7, 2005 6:09
How do i search and replace all the occurance of < with < , > with >, " with ", & with &, etc..
Basically i want to convert(un-escape)  these xml escaped file by one key stroke. Prabably i want to write a map which will call a function which does all the substitutions at once for a complete file.
[email protected], November 15, 2005 0:05
Extra question conserning replacing , by newlines:
I'm used to Ctrl-V Ctrl-M, but in gvim on windows Ctrl-V just pastes the clipboard on the after the :%s/

I tried unmap <C-V>: this disables C-V = paste *except* when in edit-mode or after a colon

Any other ideas to make Ctrl-V Ctrl-M work in gvim on windows are very welcome
[email protected], December 7, 2005 1:50
I found :1,$s/,/\r/g worked fine.  Can't get the usual Unix "^v ^M" to work though.  But at least the \r works :)
[email protected], January 2, 2006 22:48
To karel
In gvim on windows , you  can also use "Ctrl-Q" to replace "Ctrl-V".

[email protected], February 17, 2006 8:29
How can I search & replace a case-insensitive pattern and replace with a corresponding case-sensitive target.  For example, if I have the text "Hello hello HELLO" what command can I issue to change it to "Goodbye goodbye GOODBYE"?  Note that the relative positioning of lowercase and uppercase characters are identical in the original and modified text.  Perhaps I can't use the substitute command for this.
[email protected], February 28, 2006 3:22
Seems like I am a little bit blind today. I read about five good PDF's about working with vim and read also a lot of sites in the net... But I can't find the way, how to search and replace an expression like http://www.somewhat.com/somwhere etc. How do I find patterns with slashs??? I get "E488: Trailing charakters" or "The pattern http:\/\/www.subjektiv-news.de\/index could not be found" ...
I will go on searching, but if someone knows the answer - please mail me quickly! Thxs a lot!!! Jochen
[email protected], March 3, 2006 23:35
Use a different separator, such a single quote -

:%s'http://www.somewhat.com/somwhere'http://www.nowhwere.com'
[email protected], June 16, 2006 8:27
This one threw me for a loop, and thought I'd contribute in case someone else is having the same problem.

The web page I'm working on has a number of key-strokes, eg: <Alt>+<F>
But I wanted it to look like this: <Alt> + <F> (with the spaces being non-breaking spaces.)
So I wanted to search for >+< and replace with > + <

The trick is that you don't escape the ampersands in the search part, but you have to for the replace part, otherwise it'll do some crazy concatinatin.

%s/>+</\>\ +\ \</g
[email protected], June 16, 2006 8:28
This one threw me for a loop, and thought I'd contribute in case someone else is having the same problem.

The web page I'm working on has a number of key-strokes, eg: <Alt>+<F>
But I wanted it to look like this: <Alt> + <F> (with the spaces being non-breaking spaces.)
So I wanted to search for >+< and replace with > + <

The trick is that you don't escape the ampersands in the search part, but you have to for the replace part, otherwise it'll do some crazy concatinatin.
<pre>
%s/>+</\>\ +\ \</g
</pre>
[email protected], June 16, 2006 8:32
Let's try this one more time:

:%s/>+&am;lt;/\&am;gt;\ +\ \</g
[email protected], June 16, 2006 8:33
Let's try this one more time:

:%s/>+</\>\ +\ \</g
[email protected], August 29, 2006 11:20
Okay... the big one for me was that I couldn't search and replace a string that contained a forward slash... couldn't escape it, put it in quotation marks, nothing! But changing the separator worked! ie:

:%s'ezra/media/shoreline'shoreline'g

NICE!
[email protected], December 12, 2006 7:50
Can anyone tell me how to replace text using :%s BUT to insert the contents of a register as the replacement text?

i.e. the command would look like this

:%s/text to replace/register contents/gI

Many thanks in advance

James Gilbert
[email protected], December 12, 2006 7:52
ps. In this case using <C-R><C-V> to pull the register contents onto the command line wouldn't work (I don't think?) as the register will contain over 10 lines of text...
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo