label:
% s / foo / bar / g
Find ‘foo’ in all lines and replace with ‘bar’
: s / foo / bar / g
Find ‘foo’ in the current line and replace with ‘foo’
:% s / foo / bar / gc
Replace each ‘foo’ with ‘bar’, but ask when replacing
% s / \ <foo \> / bar / gc
Find the word exactly matches ‘foo’ with bar, but ask when replacing
:% s / foo / bar / gci
Find ‘foo’ and replace it with ‘bar’ but it is not case sensitive, ask when replacing,
:% s / foo \ c / bar / gc is the same as the previous one, because \ c is case-insensitive
Use: set noingorecase is case sensitive when searching
:% s / foo / bar / gcI
Replace each ‘foo’ with ‘bar’, case sensitive, ask when replacing
: $ s / foo \ C / bar / gcI is the same as the previous one
Use: set ignorecase is not case sensitive when searching
g means global (global)-what appears on each line is changed, not only the first one on each line
The assumption for this is that the default ‘gdefault’ and ’edcompatible’ are off, so you need to:% s /// g use global replacement
When using: set gdefault,:% s /// is to use global replacement, but:% s /// g is not
Using the g option mainly means the opposite
When using the c option, confirmation is required for each replacement operation. Vim may output replace with foobar (y / n / a / q / l / ^ E / ^ Y) ?, where foobar is:% s / ... / ..., you can use y to replace, n jump After this replacement, a replaces all current and future operations (a is all remaining matches), q exits this operation, l replaces the current operation and then exits, (l is last), and ^ E refers to pressing Press Ctrl + E to scroll to the previous screen, ^ Y means Ctrl + Y to scroll to the next screen, however the last two options are optional.
Still using the c flag, Vim will jump to the first match it finds from the top, prompting you to confirm the replacement of the match. Vim IncSearch highlights the matching text and gives you a visual cue of which match to operate on.
Search range
: s / foo / bar Replace the current line ‘foo’ with ‘bar’
:% s / foo / bar / g replace all "foo" of all lines with "bar"
: 5,12s / foo / bar / g Replace all "foo" with "bar" in lines 5-12 (including lines 5 and 12)
: ‘A,’ bs / foo / bar / g replaces ‘foo’ with ‘bar’ in all lines from mark a to mark b
: ‘<,‘> S / foo / bar / g When using + visual, it is in the area selected by visual. Vim will automatically join (‘<,‘>) when you select an area and press :.
:., $ s / foo / bar / g replace ‘foo’ with ‘bar’ from the current line (.) to the last line ($)
:., + 2s / foo / bar / g From the current line (.) To the next two lines, replace ‘foo’ with ‘bar’
: g / ^ baz / s / foo / bar / g Replace "foo" with "bar" in all lines beginning 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'>) are 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 is used in the pattern like:: '<,'> s / \% Vfoo / bar / g.
When using search:
., *, \, [, ^, And $ are metacharacters
+,?, |, &, {, (, and) must be escaped to use their special function.
\ / Is / (use backslash + slash to find slash)
\ t is a tab, \ s is a space
\ n is a new line, \ r is CR (carriage return = Ctrl + M = ^ M)
[] The content between the square brackets indicates a set of content to be found. The range of characters can be one-. For example, to find a character a, b, c, or the number 1, you can use [1a-c] to indicate , ^ Means reverse, for example [^ 1a-c] means match all characters except 1, a, b, c
\ {# \} Is used to match the remaining characters, / foo. \ {2 \} will match foo and the next two characters, \ is not necessary, /foo.{2} will have the same result
\ (foo \) creates a backtracking reference to ‘foo’, the parentheses do not have a \ to indicate a match, so \ is necessary
When using replacement:
\ r means a new line, \ n means no characters
\ & Express &
\ 0 inserts the text matched by the entire pattern
\ 1Insert the first backtracking reference, \ 2 means insert the second backtracking reference
You can also use other separators to find, for example
: s # http: //www.example.com/index.html#http: //example.com/#
Use \ zs and \ ze to specify the beginning and end of the replacement, such as
: s / Copyright 2007 All Rights Reserved / Copyright 2008 All Rights Reserved /
Can use
: s / Copyright \ zs2007 \ ze All Rights Reserved / 2008 /
Use current word or register
:% s // bar / g
Replace the last matching pattern with ‘bar’
:% s / foo / <c-r> <c-w> / g
Replace each occurrence of ‘foo’ with the word under the cursor.
<c-r> <c-w> means that you press Ctrl-R then Ctrl-W.
The word under the cursor will be inserted as though you typed it.
:% s / foo / <c-r> <c-a> / g
Replace each occurrence of ‘foo’ with the WORD under the cursor (delimited by whitespace).
<c-r> <c-a> means that you press Ctrl-R then Ctrl-A.
The WORD under the cursor will be inserted as though you typed it.
:% s / foo / <c-r> a / g
Replace each occurrence of ‘foo‘ with the contents of register ‘a’.
<c-r> a means that you press Ctrl-R then a.
The contents of register ‘a‘ will be inserted as though you typed it.
:% s / foo / <c-r> 0 / g
Same as above, using register 0 which contains the text from the most recent yank command. Examples of yank (copy) commands are yi (which copies the text inside parentheses around the cursor, and y $ which copies the text from the cursor to the end of the line. After a yank command which did not specify a destination register, the copied text can be entered by pressing Ctrl-R then 0.
:% 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 // <c-r> // g
Replace each match of the last search pattern with the / register (the last search pattern).
After pressing Ctrl-R then / to insert the last search pattern (and before pressing Enter to perform the command), you could edit the text to make any required change.
:% s / <c-r> * / bar / g
Replace all occurrences of the text in the system clipboard (in the * register) with ‘bar‘ (see next example if multiline).
On some systems, selecting text (in Vim or another application) is all that is required to place that text in the * register.
:% s / <c-r> a / bar / g
Replace all occurrences of the text in register ‘a‘ with ‘bar’.
<c-r> a means that you press Ctrl-R then a. The contents of register ‘a‘ will be inserted as though you typed it.
Any newlines in register ‘a‘ are inserted as ^ M and are not found.
The search works if each ^ M is manually replaced with ‘\ n’ (two characters: backslash, ‘n‘).
This replacement can be performed while you type the command:
:% s / <c-r> = substitute (@a, "\ n", ‘\\ n‘, ‘g’) <CR> / bar / g
The "\ n" (double quotes) represents the single character newline; the ‘\\ n‘ (single quotes) represents two backslashes followed by ‘n’.
The substitute () function is evaluated by the <c-r> = (Ctrl-R =) expression register; it replaces each newline with a single bac
kslash followed by ‘n’.
The <CR> indicates that you press Enter to finish the = expression.
:% s / <c-r> 0 / bar / g
Same as above, using register 0 which contains the text from the most recent yank command
Examples of use:
:% s / foo / bar /
Replace the first occurrence of ‘foo’ in each line with ‘bar’
:% s /.* \ zsfoo / bar /
Replace the last occurrence of ‘foo’ in each line with ‘bar’
:% s / \ <foo \> // g
For each line, delete all occurrences of ‘foo’
:% s / \ <foo \>. * //
Delete ‘foo’ that appears on each line and all text after this line
:% s / \ <foo \>. \ {5} //
Delete the "foo" appearing on each line and the next 5 characters
:% s / \ <foo \> \ zs. * //
Delete each line of text that appears after ‘foo’
:% s /.* \ <foo \> //
Delete ‘foo’ in each line and the text before it
:% s /.* \ ze \ <foo \> //
Delete every line of text that appears before ‘foo’
:% s /.* \ (\ <foo \> \). * / \ 1 /
Delete the text before and after each line of "foo"
: s / ^ \ (\ w \) / \ u \ 1 /
Capitalize the first character of the current line
:% s / \ (. * \ n \) \ {5 \} / & \ r /
Insert a blank line every 5 lines
Repeat \ (. * \ N \) (any line) 5 times (\ {5 \})
Replace with & (the matched text is 5 lines), followed by a new line (\ r)
:% s / \ <foo \ (\ a * \) \> / \ = len (add (list, submatch (1)))? submatch (0): submatch (0) / g
Get a list of matching results, the list must exist
Set the modified flag, because the replacement, but the content has not changed
:% s / \ <foo \ (\ a * \) \> / \ = add (list, submatch (1)) / gn
This has the advantage, that the buffer wo n‘t be 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.
Better learning materials
vim-find and replace