snipt

Ctrl+h for KB shortcuts
Notice: Snipt is moving away from free accounts on May 1st, 2014. Read more about the transition here.
#34609

VimL

Vim search and replace reference.

Search range:
:s/foo/bar/g	 Change each 'foo' to 'bar' in the current line.
:%s/foo/bar/g	 Change each 'foo' to 'bar' in all lines.
:5,12s/foo/bar/g	 Change each 'foo' to 'bar' for all lines from line 5 to line 12 inclusive.
:'a,'bs/foo/bar/g	 Change each 'foo' to 'bar' for all lines from mark a to mark b inclusive.
:.,$s/foo/bar/g	 Change each 'foo' to 'bar' for all lines from the current line (.) to the last line ($) inclusive.
:.,+2s/foo/bar/g	 Change each 'foo' to 'bar' for the current line (.) and the two next lines (+2).
:%s/foo/bar/g	 Equivalent to :1,$s/foo/bar/g (change all lines).
:g/^baz/s/foo/bar/g	 Change each 'foo' to 'bar' in each line starting with 'baz'.
When searching:
., *, \, [, ], ^, and $ are metacharacters.
+, ?, |, {, }, (, and ) must be escaped to use their special function.
\/ 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)
\{#\} is used for repetition. /foo.\{2\} will match foo and the two following characters. The \ is not required on the closing } so /foo.\{2} will do the same thing.
\(foo\) makes a backreference to foo. Parenthesis without escapes are literally matched. Here the \ is required for the closing \).
When replacing:
\r is newline, \n is a null byte (0x00).
\& is ampersand (& is the text that matches the search pattern).
\1 inserts the text of the first backreference. \2 inserts the second backreference, and so on.
You can use other delimiters with substitute:
:s#http://www.example.com/index.html#http://example.com/#
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/
Use:
:s/Copyright \zs2007\ze All Rights Reserved/2008/
https://snipt.net/embed/2e1a7153a3020838b0ba3586a7a9b419/
https://snipt.net/raw/2e1a7153a3020838b0ba3586a7a9b419/
2e1a7153a3020838b0ba3586a7a9b419
vim
VimL
27
2014-04-23T10:21:44
True
False
/api/public/snipt/34609/
vim-search-and-replace-reference
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a> <a href="#L-13">13</a> <a href="#L-14">14</a> <a href="#L-15">15</a> <a href="#L-16">16</a> <a href="#L-17">17</a> <a href="#L-18">18</a> <a href="#L-19">19</a> <a href="#L-20">20</a> <a href="#L-21">21</a> <a href="#L-22">22</a> <a href="#L-23">23</a> <a href="#L-24">24</a> <a href="#L-25">25</a> <a href="#L-26">26</a> <a href="#L-27">27</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a>Search range: </span><span id="L-2"><a name="L-2"></a><span class="p">:</span><span class="k">s</span><span class="sr">/foo/</span><span class="k">bar</span>/<span class="k">g</span> Change each <span class="s1">&#39;foo&#39;</span> <span class="k">to</span> <span class="s1">&#39;bar&#39;</span> <span class="k">in</span> the current line. </span><span id="L-3"><a name="L-3"></a><span class="p">:</span>%<span class="k">s</span><span class="sr">/foo/</span><span class="k">bar</span>/<span class="k">g</span> Change each <span class="s1">&#39;foo&#39;</span> <span class="k">to</span> <span class="s1">&#39;bar&#39;</span> <span class="k">in</span> <span class="k">all</span> <span class="nb">lines</span>. </span><span id="L-4"><a name="L-4"></a><span class="p">:</span><span class="m">5</span><span class="p">,</span><span class="m">12</span>s<span class="sr">/foo/</span><span class="k">bar</span>/<span class="k">g</span> Change each <span class="s1">&#39;foo&#39;</span> <span class="k">to</span> <span class="s1">&#39;bar&#39;</span> <span class="k">for</span> <span class="k">all</span> <span class="nb">lines</span> from line <span class="m">5</span> <span class="k">to</span> line <span class="m">12</span> inclusive. </span><span id="L-5"><a name="L-5"></a><span class="p">:</span><span class="s1">&#39;a,&#39;</span><span class="nb">bs</span><span class="sr">/foo/</span><span class="k">bar</span>/<span class="k">g</span> Change each <span class="s1">&#39;foo&#39;</span> <span class="k">to</span> <span class="s1">&#39;bar&#39;</span> <span class="k">for</span> <span class="k">all</span> <span class="nb">lines</span> from mark <span class="k">a</span> <span class="k">to</span> mark <span class="k">b</span> inclusive. </span><span id="L-6"><a name="L-6"></a><span class="p">:</span>.<span class="p">,</span>$<span class="k">s</span><span class="sr">/foo/</span><span class="k">bar</span>/<span class="k">g</span> Change each <span class="s1">&#39;foo&#39;</span> <span class="k">to</span> <span class="s1">&#39;bar&#39;</span> <span class="k">for</span> <span class="k">all</span> <span class="nb">lines</span> from the current line <span class="p">(</span>.<span class="p">)</span> <span class="k">to</span> the last line <span class="p">(</span>$<span class="p">)</span> inclusive. </span><span id="L-7"><a name="L-7"></a><span class="p">:</span>.<span class="p">,+</span><span class="m">2</span>s<span class="sr">/foo/</span><span class="k">bar</span>/<span class="k">g</span> Change each <span class="s1">&#39;foo&#39;</span> <span class="k">to</span> <span class="s1">&#39;bar&#39;</span> <span class="k">for</span> the current line <span class="p">(</span>.<span class="p">)</span> and the two next <span class="nb">lines</span> <span class="p">(+</span><span class="m">2</span><span class="p">)</span>. </span><span id="L-8"><a name="L-8"></a><span class="p">:</span>%<span class="k">s</span><span class="sr">/foo/</span><span class="k">bar</span><span class="sr">/g Equivalent to :1,$s/</span>foo<span class="sr">/bar/</span><span class="k">g</span> <span class="p">(</span>change <span class="k">all</span> <span class="nb">lines</span><span class="p">)</span>. </span><span id="L-9"><a name="L-9"></a><span class="p">:</span><span class="k">g</span><span class="sr">/^baz/</span><span class="k">s</span><span class="sr">/foo/</span><span class="k">bar</span>/<span class="k">g</span> Change each <span class="s1">&#39;foo&#39;</span> <span class="k">to</span> <span class="s1">&#39;bar&#39;</span> <span class="k">in</span> each line starting with <span class="s1">&#39;baz&#39;</span>. </span><span id="L-10"><a name="L-10"></a>When searching: </span><span id="L-11"><a name="L-11"></a>.<span class="p">,</span> *<span class="p">,</span> \<span class="p">,</span> [<span class="p">,</span> ]<span class="p">,</span> ^<span class="p">,</span> and $ are metacharacters. </span><span id="L-12"><a name="L-12"></a><span class="p">+,</span> ?<span class="p">,</span> <span class="p">|,</span> {<span class="p">,</span> }<span class="p">,</span> <span class="p">(,</span> and <span class="p">)</span> must be escaped <span class="k">to</span> use their special <span class="k">function</span>. </span><span id="L-13"><a name="L-13"></a>\<span class="sr">/ is /</span> <span class="p">(</span>use backslash <span class="p">+</span> forward slash <span class="k">to</span> search <span class="k">for</span> forward slash<span class="p">)</span> </span><span id="L-14"><a name="L-14"></a>\<span class="k">t</span> <span class="k">is</span> <span class="k">tab</span><span class="p">,</span> \<span class="k">s</span> <span class="k">is</span> whitespace </span><span id="L-15"><a name="L-15"></a>\<span class="k">n</span> <span class="k">is</span> newline<span class="p">,</span> \<span class="k">r</span> <span class="k">is</span> CR <span class="p">(</span>carriage <span class="k">return</span> <span class="p">=</span> Ctrl<span class="p">-</span>M <span class="p">=</span> ^M<span class="p">)</span> </span><span id="L-16"><a name="L-16"></a>\{#\} <span class="k">is</span> used <span class="k">for</span> repetition. <span class="sr">/foo.\{2\} will match foo and the two following characters. The \ is not required on the closing } so /</span>foo.\{<span class="m">2</span>} will <span class="k">do</span> the same thing. </span><span id="L-17"><a name="L-17"></a>\<span class="p">(</span>foo\<span class="p">)</span> makes <span class="k">a</span> backreference <span class="k">to</span> foo. Parenthesis without escapes are literally matched. Here the \ <span class="k">is</span> required <span class="k">for</span> the closing \<span class="p">)</span>. </span><span id="L-18"><a name="L-18"></a>When replacing: </span><span id="L-19"><a name="L-19"></a>\<span class="k">r</span> <span class="k">is</span> newline<span class="p">,</span> \<span class="k">n</span> <span class="k">is</span> <span class="k">a</span> null byte <span class="p">(</span><span class="m">0</span>x<span class="m">00</span><span class="p">)</span>. </span><span id="L-20"><a name="L-20"></a>\&amp; <span class="k">is</span> ampersand <span class="p">(</span>&amp; <span class="k">is</span> the text that matches the search pattern<span class="p">)</span>. </span><span id="L-21"><a name="L-21"></a>\<span class="m">1</span> inserts the text of the <span class="k">first</span> backreference. \<span class="m">2</span> inserts the second backreference<span class="p">,</span> and <span class="k">so</span> <span class="k">on</span>. </span><span id="L-22"><a name="L-22"></a>You can use other delimiters with substitute: </span><span id="L-23"><a name="L-23"></a><span class="p">:</span><span class="k">s</span>#http:<span class="sr">//</span>www.example.<span class="k">com</span><span class="sr">/index.html#http://example.com/</span># </span><span id="L-24"><a name="L-24"></a>Save typing by using \zs and \ze <span class="k">to</span> <span class="k">set</span> the <span class="k">start</span> and <span class="k">end</span> of <span class="k">a</span> pattern. For example<span class="p">,</span> instead of: </span><span id="L-25"><a name="L-25"></a><span class="p">:</span><span class="k">s</span><span class="sr">/Copyright 2007 All Rights Reserved/</span>Copyright <span class="m">2008</span> All Rights Reserved/ </span><span id="L-26"><a name="L-26"></a>Use: </span><span id="L-27"><a name="L-27"></a><span class="p">:</span><span class="k">s</span><span class="sr">/Copyright \zs2007\ze All Rights Reserved/</span><span class="m">2008</span>/ </span></pre></div> </td></tr></table>
find, replace, search, vim