Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (38 sloc) 2.28 KB
<h3 id="searching-and-replacing">Searching and Replacing</h3>
<p>You want to replace all occurrences of one string with another.</p>
<p>For example, imagine you were using Vim to write a novel wherein there was
a protaganist named <i>Curtis</i>. You decide to change his name to
<i>Excalibur</i> to give him more panache. You want Vim to make these changes
with the least possible effort.</p>
<p>Execute <tt>:%s/<var>target</var>/<var>replacement</var>/g</tt> to replace
all occurrences of <var>target</var> with <var>replacement</var>. For example:
<tt>:%s/Curtis/Excalibur/g</tt>. </p>
<p>The solution above replaces <i>Curtis</i> with <i>Excalibur</i>. The
<tt>g</tt> flag at the end causes this operation to be performed
<i>g</i>lobally, i.e. multiple times on each line, if necessary.</p>
<p>The <tt>%</tt> prefix indicates that the replacement should occur over the
entire file. Had you previously visually selected text to which you want to
constrain the search/replace operation omit the <tt>%</tt>, e.g.
<tt>:s/Curtis/Excalibur/g</tt>. Or, prefix the command with a range to
contstrain it that way, e.g. <tt>2,20s/Curtis/Excalibur/g</tt> performs the
operation over lines two to twenty.</p>
<p>The search string (<i>Curtis</i> in this example) doesn't have to be a
literal string; it can be any Vim regular expression. The details of Vim's
regular expressions are explained in the <a
href="#creating-regular-expressions"><i>Creating Regular Expressions</i></a>
recipe, so I won't go into them here. Two tips, though: </p>
<li>Search for words - If you provide a literal string to replace, as above,
Vim even replaces occurrences that form parts of other words. For example
<tt>:%s/and/or/g</tt> would change <i>supply and demand</i> into <i>supply
or demor</i>. To avoid this, surround the string with <i>word boundary
anchors</i> (<tt>\&lt;</tt>, <tt>\&gt;</tt>). Thus, the above command
could be rewritten as <tt>:%s/\&lt;and\&gt;/or/g</tt>.</li>
<li>Search case insensitively - If you want <i>linux</i> to match
<i>Linux</i>, <i>linux</i>, and <i>LiNuX</i> you need to either add the
<tt>i</tt> flag to the end of the replacement command, or, use <tt>:set
ignorecase</tt> to enable this behaviour for all searches.</li>
Something went wrong with that request. Please try again.