Vim Tips Wiki

Editing

Switching case of characters

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 12: Line 12:
 
}}
 
}}
 
You can change the case of text:
 
You can change the case of text:
:Toggle case "<code>HellO</code>" to "<code>hELLo</code>" with <code>g~</code> then a movement.
+
:Toggle case "<tt>HellO</tt>" to "<tt>hELLo</tt>" with <tt>g~</tt> then a movement.
:Uppercase "<code>HellO</code>" to "<code>HELLO</code>" with <code>gU</code> then a movement.
+
:Uppercase "<tt>HellO</tt>" to "<tt>HELLO</tt>" with <tt>gU</tt> then a movement.
:Lowercase "<code>HellO</code>" to "<code>hello</code>" with <code>gu</code> then a movement.
+
:Lowercase "<tt>HellO</tt>" to "<tt>hello</tt>" with <tt>gu</tt> then a movement.
   
Alternatively, you can visually select text then press <code>~</code> to toggle case, or <code>U</code> to convert to uppercase, or <code>u</code> to convert to lowercase.
+
Alternatively, you can visually select text then press <tt>~</tt> to toggle case, or <tt>U</tt> to convert to uppercase, or <tt>u</tt> to convert to lowercase.
   
 
==Examples==
 
==Examples==
   
; <code>~</code>
+
; <tt>~</tt>
 
: Toggle case of the character under the cursor, or all visually-selected characters.
 
: Toggle case of the character under the cursor, or all visually-selected characters.
   
; <code>3~</code>
+
; <tt>3~</tt>
 
: Toggle case of the next three characters.
 
: Toggle case of the next three characters.
   
; <code>g~3w</code>
+
; <tt>g~3w</tt>
 
: Toggle case of the next three words.
 
: Toggle case of the next three words.
   
; <code>g~iw</code>
+
; <tt>g~iw</tt>
 
: Toggle case of the current word (inner word &ndash; cursor anywhere in word).
 
: Toggle case of the current word (inner word &ndash; cursor anywhere in word).
   
; <code>g~$</code>
+
; <tt>g~$</tt>
 
: Toggle case of all characters to end of line.
 
: Toggle case of all characters to end of line.
   
; <code>g~~</code>
+
; <tt>g~~</tt>
: Toggle case of the current line (same as <code>V~</code>).
+
: Toggle case of the current line (same as <tt>V~</tt>).
   
 
The above uses ~ to toggle case. In each example, you can replace ~ with u to convert to lowercase, or with U to convert to uppercase. For example:
 
The above uses ~ to toggle case. In each example, you can replace ~ with u to convert to lowercase, or with U to convert to uppercase. For example:
   
; <code>U</code>
+
; <tt>U</tt>
 
: Uppercase the visually-selected text.
 
: Uppercase the visually-selected text.
: First press <code>v</code> or <code>V</code> then move to select text.
+
: First press <tt>v</tt> or <tt>V</tt> then move to select text.
: If you don't select text, pressing <code>U</code> will undo all changes to the current line.
+
: If you don't select text, pressing <tt>U</tt> will undo all changes to the current line.
   
; <code>gUU</code>
+
; <tt>gUU</tt>
: Change the current line to uppercase (same as <code>VU</code>).
+
: Change the current line to uppercase (same as <tt>VU</tt>).
   
; <code>gUiw</code>
+
; <tt>gUiw</tt>
 
: Change current word to uppercase.
 
: Change current word to uppercase.
   
; <code>u</code>
+
; <tt>u</tt>
 
: Lowercase the visually-selected text.
 
: Lowercase the visually-selected text.
: If you don't select text, pressing <code>u</code> will undo the last change.
+
: If you don't select text, pressing <tt>u</tt> will undo the last change.
   
; <code>guu</code>
+
; <tt>guu</tt>
: Change the current line to lowercase (same as <code>Vu</code>).
+
: Change the current line to lowercase (same as <tt>Vu</tt>).
   
 
==Title case==
 
==Title case==
The <code>:s</code> substitute command can change case (see {{help|s/\u}}).
+
The <tt>:s</tt> substitute command can change case (see {{help|s/\u}}).
   
 
The following converts the current line to Title Case (all lowercase, except for initial uppercase letters):
 
The following converts the current line to Title Case (all lowercase, except for initial uppercase letters):
  +
 
<pre>
 
<pre>
 
:s/\<\(\w\)\(\w*\)\>/\u\1\L\2/g
 
:s/\<\(\w\)\(\w*\)\>/\u\1\L\2/g
 
</pre>
 
</pre>
   
'''Explanation''' The search pattern is <code>\<\(\w\)\(\w*\)\></code> which searches for <code>\<</code> (beginning of word), then <code>\w</code> (a word character), then <code>\w*</code> (zero or more word characters), then <code>\></code> (end of word). The <code>\(...\)</code> create subexpressions to be recalled with <code>\1</code> and <code>\2</code> in the replacement. The replacement is <code>\u\1\L\2</code> which substitutes the two subexpressions transformed: The <code>\u</code> converts the first character of what follows to uppercase, while <code>\L</code> converts all of what follows to lowercase.
+
'''Explanation''' The search pattern is <tt>\<\(\w\)\(\w*\)\></tt> which searches for <tt>\<</tt> (beginning of word), then <tt>\w</tt> (a word character), then <tt>\w*</tt> (zero or more word characters), then <tt>\></tt> (end of word). The <tt>\(...\)</tt> create subexpressions to be recalled with <tt>\1</tt> and <tt>\2</tt> in the replacement. The replacement is <tt>\u\1\L\2</tt> which substitutes the two subexpressions transformed: The <tt>\u</tt> converts the first character of what follows to uppercase, while <tt>\L</tt> converts all of what follows to lowercase.
 
This approach has shortcomings in cases where words may contain what the regular expression recognizes as non-word characters, such as an apostrophe in "<kbd>I'll</kbd>" or "<kbd>she's</kbd>". An alternative based on whitespace for word boundaries is:
 
<pre>
 
:s/\<\(\w\)\(\S*\)/\u\1\L\2/g
 
</pre>
 
 
Another alternative is to replace the separator with # and use <code>\v</code> (very magic) to reduce the need to escape characters.
 
<pre>
 
:s#\v(\w)(\S*)#\u\1\L\2#g
 
</pre>
 
   
 
==Twiddle case==
 
==Twiddle case==
With the following (for example, in [[vimrc]]), you can visually select text then press <code>~</code> to convert the text to <code>UPPER CASE</code>, then to <code>lower case</code>, then to <code>Title Case</code>. Keep pressing <code>~</code> until you get the case you want.
+
With the following (for example, in [[vimrc]]), you can visually select text then press <tt>~</tt> to convert the text to <tt>UPPER CASE</tt>, then to <tt>lower case</tt>, then to <tt>Title Case</tt>. Keep pressing <tt>~</tt> until you get the case you want.
   
 
<pre>
 
<pre>
Line 82: Line 83:
 
return result
 
return result
 
endfunction
 
endfunction
vnoremap ~ y:call setreg('', TwiddleCase(@"), getregtype(''))<CR>gv""Pgv
+
vnoremap ~ ygv"=TwiddleCase(@")<CR>Pgv
 
</pre>
 
</pre>
   
Line 95: Line 96:
 
</pre>
 
</pre>
 
--Jenny [[Special:Contributions/165.2.186.10|165.2.186.10]] 19:05, April 5, 2012 (UTC)
 
--Jenny [[Special:Contributions/165.2.186.10|165.2.186.10]] 19:05, April 5, 2012 (UTC)
:Nice, thanks. I added <code>\C</code> to your command above to make the search case sensitive (it won't skip lowercase words if <code>'ignorecase'</code> is set, unless <code>\C</code> is present). [[User:JohnBeckett|JohnBeckett]] 09:58, April 6, 2012 (UTC)
+
:Nice, thanks. I added <tt>\C</tt> to your command above to make the search case sensitive (it won't skip lowercase words if <tt>'ignorecase'</tt> is set, unless <tt>\C</tt> is present). [[User:JohnBeckett|JohnBeckett]] 09:58, April 6, 2012 (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