Editing (section) Search and replace 0 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. ==Using the current word or registers== {{anchor|substitute_last_search}} ;<code>:%s//bar/g</code> :Replace each match of the last search pattern with 'bar'. :For example, you might first place the cursor on the word <code>foo</code> then press <code>*</code> to search for that word. :The above substitute would then change all words exactly matching 'foo' to 'bar'. ;<code>:%s/foo/<c-r><c-w>/g</code> :Replace each occurrence of 'foo' with the word under the cursor. :<code><c-r><c-w></code> means that you press Ctrl-R then Ctrl-W. :The word under the cursor will be inserted as though you typed it. ;<code>:%s/foo/<c-r><c-a>/g</code> :Replace each occurrence of 'foo' with the WORD under the cursor (delimited by whitespace). :<code><c-r><c-a></code> means that you press Ctrl-R then Ctrl-A. :The WORD under the cursor will be inserted as though you typed it. ;<code>:%s/foo/<c-r>a/g</code> :Replace each occurrence of 'foo' with the contents of register 'a'. :<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. ;<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> :Replace each occurrence of 'foo' with the contents of register 'a'. :<code>\=@a</code> 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. ;<code>:%s//<c-r>//g</code> :Replace each match of the last search pattern with the <code>/</code> register (the last search pattern). :After pressing Ctrl-R then <code>/</code> to insert the last search pattern (and before pressing Enter to perform the command), you could edit the text to make any required change. ;<code>:%s/<c-r>*/bar/g</code> :Replace all occurrences of the text in the system clipboard (in the <code>*</code> 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 <code>*</code> register. ;<code>:%s/<c-r>a/bar/g</code> :Replace all occurrences of the text in register 'a' with 'bar'. :<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. :Any newlines in register 'a' are inserted as <code>^M</code> and are not found. :The search works if each <code>^M</code> is manually replaced with '\n' (two characters: backslash, 'n'). :This replacement can be performed while you type the command: ::<code>:%s/<c-r>=substitute(@a,"\n",'\\n','g')<CR>/bar/g</code> :The <code>"\n"</code> (double quotes) represents the single character newline; the <code>'\\n'</code> (single quotes) represents two backslashes 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. ;<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]]. 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. Insert: – — … ° ≈ ≠ ≤ ≥ ± − × ÷ ← → · § Sign your username: ~~~~ Wiki markup: {{}} | [] [[]] [[Category:]] #REDIRECT [[]] <s></s> <sup></sup> <sub></sub> <code></code> <blockquote></blockquote> <ref></ref> {{Reflist}} <references/> <includeonly></includeonly> <noinclude></noinclude> {{DEFAULTSORT:}} <nowiki></nowiki> <!-- --> <span class="plainlinks"></span> Symbols: ~ | ¡ ¿ † ‡ ↔ ↑ ↓ • ¶ # ¹ ² ³ ½ ⅓ ⅔ ¼ ¾ ⅛ ⅜ ⅝ ⅞ ∞ ‘ “ ’ ” «» ¤ ₳ ฿ ₵ ¢ ₡ ₢ $ ₫ ₯ € ₠ ₣ ƒ ₴ ₭ ₤ ℳ ₥ ₦ № ₧ ₰ £ ៛ ₨ ₪ ৳ ₮ ₩ ¥ ♠ ♣ ♥ ♦ View this template This field is a spam trap. DO NOT fill it in! Edit summary Preview Mobile Desktop Show changes