Recent changes Random page
GAMING
Technology
 
Gaming
Entertainment
Science Fiction
Biggest wikis
Hobbies
Music
See more...

Search and replace

From Vim Tips Wiki

(Redirected from VimTip31)
Jump to: navigation, search

Tip 31 Previous Next created March 7, 2001 · complexity basic · author Anon · version 5.7


Use the :substitute command to search for a text pattern, and replace it with a text string.

There are many options, but these are what you most probably want:

:%s/foo/bar/g
Find each occurrence of 'foo', and replace it with 'bar'.
:%s/foo/bar/gc
Change each 'foo' to 'bar', but 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 each 'foo' (case insensitive) to 'bar'; ask for confirmation.
:%s/foo/bar/gcI
Change each 'foo' (case sensitive) to 'bar'; ask for confirmation.

The g flag means global — each occurrence in the line is changed, rather than just the first.

Search range:

The % in :%s applies the substitute to every line in the buffer, rather than just the current line.
:5,12s/foo/bar/g will change each 'foo' to 'bar' for all lines between line 5 and line 12.
:'a,'bs/foo/bar/g will change each 'foo' to 'bar' for all lines between marks a and b.
:.,$s/foo/bar/g will change each 'foo' to 'bar' for all lines between the current line (.) and the last line ($).
:.,+2s/foo/bar/g will change each 'foo' to 'bar' for the current line (.) and the two next lines (+2).
(Note that :%s is equivalent to :1,$s.)

When searching:

\/ 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)

When replacing:

\r is newline, \n is a null byte (0x00).
\& is ampersand (& is the search pattern).

You can use other delimiters with substitute:

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

This can be a very helpful technique, because you can avoid escaping a / character if you use something other than / for a delimiter, and some delimiters such as ; may be faster/easier to type.

Save typing by using \zs and \ze to set the start and end of a pattern. For example, instead of:

:s/Copyright 2007 All Rights Reserved/Copyright 2008 All Rights Reserved/

You could try:

:s/Copyright \zs2007\ze All Rights Reserved/2008/

[edit] Using registers

:%s/foo/<c-r>a/g
Substitute each 'foo' by the content of register 'a'.
<c-r>a means: Press Ctrl-R then A. The content of register 'a' will be immediately inserted.
:%s/foo/\=@a/g
Substitute each 'foo' by the content of register 'a'.
\=@a is a reference to register 'a'. The content of register 'a' will not be shown. This is useful if register 'a' contains many lines of text.
:%s/<c-r>//bar/g
Substitute the last search pattern (<c-r>/) by 'bar'.
Hint: If the substitute pattern is omitted, the last search pattern is used. See 'r' flag.
:%s/<c-r>*/bar/g
Substitute the visual selection (saved in the '*' register) by 'bar'.

See Paste registers in search or colon commands instead of using the clipboard.

[edit] Special cases

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 in 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 having to complain about offset differences.

NOTE
; Should try to make the expression more compact, but don't know how without having the possibility of modifying unwanted lines

[edit] See also – using substitute

[edit] See also – substitute in buffers/files

[edit] References

[edit] Comments

 TO DO 
The large "see also" section may be useful to readers. We need to merge some of the related tips (but don't make the result too complex). I included the tip numbers to help editors keep track.

[edit] Open questions

  • Is it possible to search for the next (or previous) unmatched and therefore highlighted brace?
  • Is it possible to use a register as a search pattern? E.g. replace the content of register 'a' by the content of register 'b':
:%s/\=@a/\=@b/g (doesn't work)
AFAIK, we can't use expressions as the {pattern} parameter of :substitute. Actually, using registers is just a special case of the general use of expression. \= is about using a dynamic expression as the replacement {string}. See :help sub-replace-special
--Luc Hermitte 11:59, 7 September 2007 (UTC)
  • If the content of register 'a' is inserted by <c-r>a, the above task can be accomplished. However, if register 'a' contains line breaks, they are inserted as ^M and not found. If they are manually replaced by '\n', they are found correctly.
Then use <c-r>=substitute(@a, "\n", '\\n', "g") instead of <c-r>a
--Luc Hermitte 11:59, 7 September 2007 (UTC)
  • Is it possible to search for a partial word and then replace by typing? e.g. search for "foo" and then replace by typing "bar" in one match and replace by typing "zoo" in another match.
Search for foo from normal mode using /foo. The cursor will move to the next instance of foo. To change foo to bar type "3sbar". To change foo to zoo type "rz". To move to the next instance of foo type "n". To repeat the last change, type ".".
Good point. Also, if you used Copy or change search hit, you could press n to find the next instance, then type cs to change the search hit to whatever.

Rate this article:
Share this article: