Vim Tips Wiki

Editing

Search and replace

1
  • The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit. If you are undoing an edit that is …
Latest revision Your text
Line 72: Line 72:
 
:<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.
 
: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.
Line 114: Line 114:
 
:<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 138:
 
: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 168:
 
;<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 236: Line 227:
   
 
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)
 
  Loading editor
Below are some commonly used wiki markup codes. Simply click on what you want to use and it will appear in the edit box above.

View this template