Vim Tips Wiki
Edit Page
We recommend that you log in before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your Watchlist. Creating an account is quick and free.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 29: Line 29:
   
 
;<code>:%s/foo/bar/gci</code>
 
;<code>:%s/foo/bar/gci</code>
:Change each 'foo' (case insensitive due to the <code>i</code> flag) to 'bar'; ask for confirmation.
+
:Change each 'foo' (case insensitive) to 'bar'; ask for confirmation.
:<code>:%s/foo\c/bar/gc</code> is the same because <code>\c</code> makes the search case insensitive.
 
 
:This may be wanted after using <code>:set noignorecase</code> to make searches case sensitive (the default).
 
:This may be wanted after using <code>:set noignorecase</code> to make searches case sensitive (the default).
   
 
;<code>:%s/foo/bar/gcI</code>
 
;<code>:%s/foo/bar/gcI</code>
:Change each 'foo' (case sensitive due to the <code>I</code> flag) to 'bar'; ask for confirmation.
+
:Change each 'foo' (case sensitive) to 'bar'; ask for confirmation.
:<code>:%s/foo\C/bar/gc</code> is the same because <code>\C</code> makes the search case sensitive.
 
 
:This may be wanted after using <code>:set ignorecase</code> to make searches case insensitive.
 
:This may be wanted after using <code>:set ignorecase</code> to make searches case insensitive.
   
Line 51: Line 49:
 
|style="vertical-align:top" |<code>:s/foo/bar/g</code> || Change each 'foo' to 'bar' in the current line.
 
|style="vertical-align:top" |<code>:s/foo/bar/g</code> || Change each 'foo' to 'bar' in the current line.
 
|-
 
|-
|style="vertical-align:top" |<code>:%s/foo/bar/g</code> || Change each 'foo' to 'bar' in all the lines.
+
|style="vertical-align:top" |<code>:%s/foo/bar/g</code> || Change each 'foo' to 'bar' in all lines.
 
|-
 
|-
|style="vertical-align:top" |<code>:5,12s/foo/bar/g</code> || Change each 'foo' to 'bar' for all lines from line 5 to line 12 (inclusive).
+
|style="vertical-align:top" |<code>:5,12s/foo/bar/g</code> || Change each 'foo' to 'bar' for all lines from line 5 to line 12 inclusive.
 
|-
 
|-
 
|style="vertical-align:top" |<code>:'a,'bs/foo/bar/g</code> || Change each 'foo' to 'bar' for all lines from mark a to mark b inclusive (see '''Note''' below).
 
|style="vertical-align:top" |<code>:'a,'bs/foo/bar/g</code> || Change each 'foo' to 'bar' for all lines from mark a to mark b inclusive (see '''Note''' below).
Line 69: Line 67:
   
 
'''When searching''':
 
'''When searching''':
:<code>.</code>, <code>*</code>, <code>\</code>, <code>[</code>, <code>^</code>, and <code>$</code> are metacharacters.
+
:<code>.</code>, <code>*</code>, <code>\</code>, <code>[</code>, <code>]</code>, <code>^</code>, and <code>$</code> are metacharacters.
:<code>+</code>, <code>?</code>, <code>|</code>, <code>&</code>, <code>{</code>, <code>(</code>, and <code>)</code> must be escaped to use their special function.
+
:<code>+</code>, <code>?</code>, <code>|</code>, <code>{</code>, <code>}</code>, <code>(</code>, and <code>)</code> must be escaped to use their special function.
 
:<code>\/</code> is / (use backslash + forward slash to search for forward slash)
 
:<code>\/</code> is / (use backslash + forward slash to search for forward slash)
:<code>\t</code> is tab, <code>\s</code> is whitespace (space or tab)
+
:<code>\t</code> is tab, <code>\s</code> is whitespace
 
:<code>\n</code> is newline, <code>\r</code> is CR (carriage return = Ctrl-M = ^M)
 
:<code>\n</code> is newline, <code>\r</code> is CR (carriage return = Ctrl-M = ^M)
:After an opening <code>[</code>, everything until the next closing <code>]</code> specifies a {{help|prefix=no|/collection}}. Character ranges can be represented with a <code>-</code>; for example a letter a, b, c, or the number 1 can be matched with <code>[1a-c]</code>. Negate the collection with <code>[^</code> instead of <code>[</code>; for example <code>[^1a-c]</code> matches any character except a, b, c, or 1.
 
 
:<code>\{#\}</code> is used for repetition. <code>/foo.\{2\}</code> will match foo and the two following characters. The <code>\</code> is not required on the closing <code>}</code> so <code>/foo.\{2}</code> will do the same thing.
 
:<code>\{#\}</code> is used for repetition. <code>/foo.\{2\}</code> will match foo and the two following characters. The <code>\</code> is not required on the closing <code>}</code> so <code>/foo.\{2}</code> will do the same thing.
 
:<code>\(foo\)</code> makes a backreference to foo. Parenthesis without escapes are literally matched. Here the <code>\</code> is required for the closing <code>\)</code>.
 
:<code>\(foo\)</code> makes a backreference to foo. Parenthesis without escapes are literally matched. Here the <code>\</code> is required for the closing <code>\)</code>.
Line 114: Line 111:
 
:<code><c-r>a</code> means that you press Ctrl-R then <code>a</code>.
 
:<code><c-r>a</code> means that you press Ctrl-R then <code>a</code>.
 
:The contents of register 'a' will be inserted as though you typed it.
 
:The contents of register 'a' will be inserted as though you typed it.
 
;<code>:%s/foo/<c-r>0/g</code>
 
:Same as above, using register 0 which contains the text from the most recent yank command. Examples of yank (copy) commands are <code>yi(</code> which copies the text inside parentheses around the cursor, and <code>y$</code> 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 <code>0</code>.
 
   
 
;<code>:%s/foo/\=@a/g</code>
 
;<code>:%s/foo/\=@a/g</code>
Line 141: Line 135:
 
:The <code>substitute()</code> function is evaluated by the <code><c-r>=</code> (Ctrl-R <code>=</code>) expression register; it replaces each newline with a single backslash followed by '<code>n</code>'.
 
:The <code>substitute()</code> function is evaluated by the <code><c-r>=</code> (Ctrl-R <code>=</code>) expression register; it replaces each newline with a single backslash followed by '<code>n</code>'.
 
:The <code><CR></code> indicates that you press Enter to finish the <code>=</code> expression.
 
:The <code><CR></code> indicates that you press Enter to finish the <code>=</code> expression.
 
;<code>:%s/<c-r>0/bar/g</code>
 
:Same as above, using register 0 which contains the text from the most recent yank command.
 
   
 
See [[VimTip490|Paste registers in search or colon commands instead of using the clipboard]].
 
See [[VimTip490|Paste registers in search or colon commands instead of using the clipboard]].
Line 174: Line 165:
 
;<code>:%s/.*\(\<foo\>\).*/\1/</code>
 
;<code>:%s/.*\(\<foo\>\).*/\1/</code>
 
:On each line, delete all the text preceding and following the whole word "foo".
 
:On each line, delete all the text preceding and following the whole word "foo".
 
;<code>:%s/\<foo\(bar\)\@!/toto/g</code>
 
:On each line, replace each occurrence of "foo" (which starts a word and is not followed by "bar") by "toto".
 
   
 
;<code>:s/^\(\w\)/\u\1/</code>
 
;<code>:s/^\(\w\)/\u\1/</code>
:If the first character at the beginning of the ''current line only'' is lowercase, switch it to uppercase using <code>\u</code> (see [[switching case of characters]]).
+
:If the first character at the beginning of the current line is lowercase, switch it to uppercase using <code>\u</code> (see [[switching case of characters]]).
   
 
;<code>:%s/\(.*\n\)\{5\}/&\r/</code>
 
;<code>:%s/\(.*\n\)\{5\}/&\r/</code>
Line 187: Line 175:
   
 
;<code>:%s/\<foo\(\a*\)\>/\=len(add(list, submatch(1)))?submatch(0):submatch(0)/g</code>
 
;<code>:%s/\<foo\(\a*\)\>/\=len(add(list, submatch(1)))?submatch(0):submatch(0)/g</code>
:Get a list of search results. (the list must exist)
+
:Get a list of search results. (the list must be exists)
:Sets the <code>modified</code> flag, because of the replacement, but the content is unchanged.
+
:Indicates the <code>modified</code> 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:
+
:Note: With a recent enough Vim (version 7.3.627 or higher), you can simplify this to:
 
;<code>:%s/\<foo\(\a*\)\>/\=add(list, submatch(1))/gn</code>
 
;<code>:%s/\<foo\(\a*\)\>/\=add(list, submatch(1))/gn</code>
 
:This has the advantage, that the buffer won'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.
 
:This has the advantage, that the buffer won'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.
   
 
==Special cases==
 
==Special cases==
For substituting patterns with corresponding case-sensitive text, Michael Geddes's {{script|id=6|text=keepcase}} plugin can be used, e.g.:
+
For substituting patterns with a corresponding case-sensitive text, Michael Geddes's {{script|id=6|text=keepcase}} plugin can be used, e.g.:
 
;<code>:%SubstituteCase/\cHello/goodBye/g</code>
 
;<code>:%SubstituteCase/\cHello/goodBye/g</code>
 
:Substitute 'Hello hello helLo HELLO' by 'Goodbye goodbye goodBye GOODBYE'
 
:Substitute 'Hello hello helLo HELLO' by 'Goodbye goodbye goodBye GOODBYE'
Line 242: Line 230:
   
 
Want a short section mentioning that simple substitutes are often best handled by searching then manually changing (and pressing <code>.</code> to repeat the last change). Additionally, you can decide how to change each instance. See [[Copy or change search hit]] for a technique where you can press <code>n</code> to find the next instance, then type <code>cs</code> to change the search hit to whatever.
 
Want a short section mentioning that simple substitutes are often best handled by searching then manually changing (and pressing <code>.</code> to repeat the last change). Additionally, you can decide how to change each instance. See [[Copy or change search hit]] for a technique where you can press <code>n</code> to find the next instance, then type <code>cs</code> to change the search hit to whatever.
----
 
Has there been a change recently with how %s works? Somehow i can use both <c-r> and \=@ as replacers, but I can't use them as searches and replacements.
 
:If you describe exactly what you do and what happens I might be able to help although see [[Vim Tips Wiki:Community Portal#Asking questions|asking questions]]. [[User:JohnBeckett|JohnBeckett]] ([[User talk:JohnBeckett|talk]]) 02:15, June 1, 2019 (UTC)
 
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel Editing help (opens in new window)