Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (38 sloc) 1.82 KB
<h3 id="changing-case-of-text">Changing the Case of Text</h3>
<h4>Problem</h4>
<p>You want to change the case of a character or block of text. For example,
you may want to change <i>bob</i> to <i>Bob</i>.</p>
<h4>Solution</h4>
<dl>
<dt><kbd>~</kbd>
<dd>Toggles the case of the current character in Normal mode, or the
selection in <a href="#visually-selecting-text"><i>Visual</i></a> mode.</dd>
<dt><kbd>u</kbd></dt>
<dd>Lowercases highlighted text. (Note: This only works in <i>Visual</i>
mode; otherwise <kbd>u</kbd> will undo your last change).</dd>
<dt><kbd>U</kbd>
<dd>Uppercases highlighted text. (Note: This only works in <i>Visual</i>
mode; otherwise <kbd>U</kbd> will undo the changes made on the current
line).</dd>
</dl>
<h4>Discussion</h4>
<p>As normal, these commands accept <a
href="#selecting-text-with-motions"><i>motions</i></a>. For example:</p>
<dl>
<dt><kbd>VU</kbd></dt>
<dd>Uppercase current line.</dd>
<dt><kbd>guw</kbd></dt>
<dd>Lowercases current word.</dd>
</dl>
<p>To convert a string to <i>title case</i>, i.e. initial capitals, you can
use the following <a href="#creating-regular-expressions">regular
expression</a>: <code>s/\&lt;\(\w\)\(\w*\)\&gt;/\u\1\L\2/g</code>. <a
href="#visually-selecting-text">Select the text</a> you want to convert, hit
<kbd>:</kbd>, then enter the regular expression. If you use this regularly,
consider <a href="#creating-keyboard-shortcuts-with-key-mappings">remapping a
key</a> to execute this command. For example:</p>
<pre><code>nnoremap &lt;F7&gt; :s/\&lt;\(\w\)\(\w*\)\&gt;/\u\1\L\2/g&lt;CR&gt;
vnoremap &lt;F7&gt; :s/\%V\(\w\)\(\w*\)\%V/\u\1\L\2/g&lt;CR&gt;</code></pre>
<p>Alternatively, you can install the <a
href="http://www.vim.org/scripts/script.php?script_id=439">titlecase</a>
<a href="#extending-vim-with-scripts-plugins">plugin</a>.</p>
Something went wrong with that request. Please try again.