Reprinted from Http://www.uhdesk.com/?p=29
He substitute command searches for a text pattern, and replaces it with a text string. There is many options, but these is what do you probably want:
:%s/foo/bar/g
Find each occurrence of ' foo ' (in all lines), and replace it with ' bar '.
: s/foo/bar/g
Find each occurrence of ' foo ' (on the current line only), and replace it with ' bar '.
:%S/FOO/BAR/GC
The change is ' foo ' to ' bar ' and ' ask for confirmation first.
:%S/\<FOO\>/BAR/GC
Change only whole words exactly matching ' foo ' to ' bar '; Ask for confirmation.
:%S/FOO/BAR/GCI
Change the ' foo ' (case insensitive) to ' bar '; Ask for confirmation.
This is wanted after using:set noignorecase to make searches case sensitive (the default).
:%S/FOO/BAR/GCI
Change the ' foo ' (case sensitive) to ' bar '; Ask for confirmation.
This is wanted after using:set ignorecase to make searches case insensitive.
The G flag means global–each occurrence in the line is changed, rather than just the first. This tip assumes the default setting for the ' Gdefault ' and ' edcompatible ' option (off), which requires that the G flag is Included in%s///g to perform a global substitute. Using:set Gdefault creates confusion because then%s///are global, whereas%s///g is not (so is, G-reverses its meaning ).
When using the-c flag, you need-confirm for each match-do. Vim would output something like:replace with Foobar (y/n/a/q/l/^e/^y)? (where Foobar is the replacement part of the:s/.../.../command. You can type Y which means to substitute this match, N to skip this match, a to substitute this and all remaining matches ("All" remaining-matches), Q to quit the command, L-substitute this match and quit (think of ' last '), ^e to scroll the Screen up is holding the CTRL key and pressing E and ^y to scroll the screen down by holding the CTRL key and pressing Y. However, the last choices was only available, if your Vim is a normal, big or huge built or the Insert_expand feature Was is enabled at compile time (look for +insert_expand in the output of:version).
Also when using the C flag, Vim would jump to the first match it finds starting from the top of the buffer and prompt you f or confirmation to perform replacement on the that match. Vim applies the incsearch highlight group to the matched text to give you a visual cue as to which match it's operating O N (set to reverse by default for all three term types as of Vim 7.3). Additionally, if more than one match are found and you have search highlighting enabled With:set Hlsearch, Vim highlights The remaining matches with the Search highlight group. If You don't use search highlighting, you should make sure that these, highlight groups is visually distinct or you won ' t Be able to easily tell which match Vim are prompting to substitute.
Detailsedit
Search Range:
: s/foo/bar/g Change each of the ' foo ' to ' bar ' on the current line.
:%s/foo/bar/g The change is ' foo ' to ' bar ' in the lines.
: 5,12s/foo/bar/g Change the ' foo ' to ' bar ' for all lines from line 5 to line (inclusive).
: ' A, ' bs/foo/bar/g Change the ' foo ' to ' bar ' for all lines from Mark A to mark B inclusive (see Note below).
: ' <, ' >s/foo/bar/g When compiled with +visual, the change is ' foo ' to ' bar ' for all lines within a visual selection. Vim automatically appends the visual selection range (' <, ' >) for any ex-command when you select a area and enter:. Also, see Note below.
:., $s/foo/bar/g Change "foo" to "bar" for all lines from the "line (.)" To the last line ($) inclusive.
:., +2s/foo/bar/g Change each of the ' foo ' to ' bar ' for the current line (.) and the next lines (+2).
: g/^baz/s/foo/bar/g Change the ' foo ' to ' bar ' in the starting with ' Baz '.
Note:as of Vim 7.3, substitutions applied to a range defined by marks or a visual selection (which uses a special type of Marks ' < and ' >) is not bounded by the column position of the marks by default. Instead, Vim applies the substitution to the entire line on which each mark appears unless the \%v atom was used in the PAT Tern like:: ' <, ' >s/\%vfoo/bar/g.
When searching:
., *, \, [,], ^, and $ are metacharacters.
+,?, |, {,}, (, and) must is escaped to the use their special function.
\/is/(use backslash + forward slash to search for forward slash)
\ t is tab, \s is whitespace
\ n is newline, \ r is CR (carriage return = Ctrl-m = ^m)
\{#\} is used for repetition. /foo.\{2\} would match foo and the following characters. The \ is isn't required on the closing} so/foo.\{2} would do the same thing.
\ (foo\) makes a backreference to Foo. Parenthesis without escapes is literally matched. Here's required for the closing \).
When replacing:
\ r is newline, \ n is a null byte (0x00).
\& is ampersand (& was the text that matches the search pattern).
Inserts the text matched by the entire pattern
\1 inserts the text of the first backreference. \2 Inserts the second backreference, and so on.
You can use the other delimiters with substitute:
: s#http://www.example.com/index.html#http://example.com/#
Save typing by using \zs and \ze to set the start and end of a pattern. For example, instead of:
: s/copyright rights reserved/copyright All rights reserved/
Use:
: S/copyright \zs2007\ze All rights reserved/2008/
Using the current word or registersedit
:%s//bar/g
Replace each match of the last search pattern with ' bar '.
For example, you might first place the cursor on the word foo and then press * to search for that word.
The above substitute would then change all words exactly matching ' foo ' to ' bar '.
:%s/foo//g
Replace each occurrence of ' foo ' with the word under the cursor.
means that's press Ctrl-r then ctrl-w.
The word under the cursor is inserted as though you typed it.
:%s/foo//g
Replace each occurrence of ' foo ' with the WORD under the cursor (delimited by whitespace).
means that's press Ctrl-r then ctrl-a.
The WORD under the cursor is inserted as though you typed it.
:%s/foo/a/g
Replace each occurrence of ' foo ' with the contents of register ' a '.
A means that's press ctrl-r then a.
The contents of register ' a ' is inserted as though you typed it.
:%s/foo/\[email protected]/g
Replace each occurrence of ' foo ' with the contents of register ' a '.
\[email protected] is a reference to register ' a '.
The contents of register ' a ' is not shown in the command. This is useful if the register contains many lines of text.
:%s////g
Replace each match of the last search pattern with the/register (the last search pattern).
After pressing Ctrl-r then/to inserts the last search pattern (and before pressing Enter to perform the command), you cou LD edit the text to do any required.
:%s/*/bar/g
Replace all occurrences of the "the text in the" system Clipboard (in the "register") with a ' bar ' (see next example if multiline) .
On some systems, selecting-Text (in Vim or another application) are all that's required to place this text in the * regist Er.
:%s/a/bar/g
Replace all occurrences of the text in register ' a ' with ' bar '.
A means that's press ctrl-r then a. The contents of register ' a ' is inserted as though you typed it.
Any newlines in register ' a ' is inserted as ^m and is not found.
The search works if each ^m are manually replaced with ' \ n ' (both Characters:backslash, ' N. ').
This replacement can is performed while you type the command:
:%s/=substitute (@a, "\ n", ' \\n ', ' g ')/bar/g
The "\ n" (double quotes) represents the single character newline; The ' \\n ' (single quotes) represents and backslashes followed by ' n '.
The substitute () function is evaluated by the = (Ctrl-r =) expression register; It replaces each newline with a single backslash followed by ' n '.
The indicates that's press Enter to finish the = expression.
See Paste registers in search or colon commands instead of using the Clipboard.
Additional Examplesedit
:%s/foo/bar/
On each line, replace the first occurrence of "foo" with "Bar".
:%s/.*\zsfoo/bar/
On all line, replace the last occurrence of "foo" with "Bar".
:%s/\<foo\>//g
On each line, delete all occurrences of the whole word "foo".
:%s/\<foo\>.*//
On each line, delete the whole word ' foo ' and ' all following text ' (to end of line).
:%s/\<foo\>.\{5}//
On each line, delete the first occurrence of the whole word "foo" and the following five characters.
:%s/\<foo\>\zs.*//
On each line, delete any text following the whole word "foo" (to end of line).
:%s/.*\<foo\>//
On each line, delete the whole word "foo" and the preceding text (from beginning of line).
:%s/.*\ze\<foo\>//
On each line, delete all the text preceding the whole word "foo" (from beginning of line).
:%s/.*\ (\<foo\>\). */\1/
On each line, delete all the text preceding and following the whole word "foo".
: s/^\ (\w\)/\u\1/
If the first character at the beginning of the "is lowercase", switch it to uppercase using \u (see switching C ASE of characters).
:%s/\ (. *\n\) \{5\}/&\r/
Insert a blank line every 5 lines.
The pattern searches for \ (. *\n\) (any line including it line ending) repeated five times (\{5\}).
The replacement is & (the text and was found), followed by \ r (newline).
:%s/\<foo\ (\a*\) \>/\=len (Add (list, Submatch (1)))? Submatch (0): submatch (0)/g
Get a list of search results. (the list must exist)
Sets the modified flag, because of the replacement, but the content is unchanged.
Note:with A recent enough Vim (version 7.3.627 or higher), you can simplify this to:
:%s/\<foo\ (\a*\) \>/\=add (list, Submatch (1))/gn
This have the advantage, that the buffer won ' t being marked modified and no extra undo state is created. The expression in the replacement part is executed in the sandbox and not allowed to modify the buffer.
Special Casesedit
For substituting patterns with a corresponding case-sensitive text, Michael Geddes's Keepcase plugin can be used, e.g.:
:%substitutecase/\chello/goodbye/g
Substitute ' Hello hello hello hello ' by ' Goodbye Goodbye Goodbye Goodbye '
For changing the offsets with a patch file (line number of a block), this little snippet can be used:
s/^@@-\ (\d\+\), \ (\d\+\) +\ (\d\+\), \ (\d\+\) @@$/\= "@@-". Eval (Submatch (1) +offsetdiff). ",". Submatch (2). "+". Eval ( Submatch (3) +offsetdiff). ",". Submatch (4). "@@"/g
Useful when we want to strip some blocks from a patch, without patch has to complain about offset differences.
The Vim editor replacement function is detailed