JohnBeckett (talk | contribs) (Undo revision 23807 by 38.116.13.2 (talk) - can't see a useful change) |
JohnBeckett (talk | contribs) (→Title case: Improve.) |
||
(19 intermediate revisions by 11 users not shown) | |||
Line 3: | Line 3: | ||
|previous=48 |
|previous=48 |
||
|next=50 |
|next=50 |
||
− | |created= |
+ | |created=2001 |
|complexity=basic |
|complexity=basic |
||
− | |author= |
+ | |author= |
|version=5.7 |
|version=5.7 |
||
|rating=102/51 |
|rating=102/51 |
||
Line 12: | Line 12: | ||
}} |
}} |
||
You can change the case of text: |
You can change the case of text: |
||
− | :Toggle case "HellO" to "hELLo" with |
+ | :Toggle case "<code>HellO</code>" to "<code>hELLo</code>" with <code>g~</code> then a movement. |
− | :Uppercase "HellO" to "HELLO" with |
+ | :Uppercase "<code>HellO</code>" to "<code>HELLO</code>" with <code>gU</code> then a movement. |
− | :Lowercase "HellO" to "hello" with |
+ | :Lowercase "<code>HellO</code>" to "<code>hello</code>" with <code>gu</code> then a movement. |
− | Alternatively, you can visually select text then press |
+ | 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. |
==Examples== |
==Examples== |
||
+ | ; <code>~</code> |
||
− | ; ~ |
||
: 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. |
||
− | ; 3~ |
+ | ; <code>3~</code> |
: Toggle case of the next three characters. |
: Toggle case of the next three characters. |
||
− | ;g~3w |
+ | ; <code>g~3w</code> |
: Toggle case of the next three words. |
: Toggle case of the next three words. |
||
− | ;g~iw |
+ | ; <code>g~iw</code> |
: Toggle case of the current word (inner word – cursor anywhere in word). |
: Toggle case of the current word (inner word – cursor anywhere in word). |
||
− | ;g~$ |
+ | ; <code>g~$</code> |
: Toggle case of all characters to end of line. |
: Toggle case of all characters to end of line. |
||
− | ;g~~ |
+ | ; <code>g~~</code> |
− | : Toggle case of the current line (same as < |
+ | : Toggle case of the current line (same as <code>V~</code>). |
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> |
||
− | ;U |
||
: Uppercase the visually-selected text. |
: Uppercase the visually-selected text. |
||
− | : First press < |
+ | : First press <code>v</code> or <code>V</code> then move to select text. |
− | : If you don't select text, pressing < |
+ | : If you don't select text, pressing <code>U</code> will undo all changes to the current line. |
− | ;gUU |
+ | ; <code>gUU</code> |
− | : Change the current line to uppercase (same as < |
+ | : Change the current line to uppercase (same as <code>VU</code>). |
− | ; gUiw |
+ | ; <code>gUiw</code> |
: Change current word to uppercase. |
: Change current word to uppercase. |
||
+ | ; <code>u</code> |
||
− | ;u |
||
: Lowercase the visually-selected text. |
: Lowercase the visually-selected text. |
||
− | : If you don't select text, pressing < |
+ | : If you don't select text, pressing <code>u</code> will undo the last change. |
− | ;guu |
+ | ; <code>guu</code> |
− | : Change the current line to lowercase (same as < |
+ | : Change the current line to lowercase (same as <code>Vu</code>). |
==Title case== |
==Title case== |
||
− | The < |
+ | The <code>:s</code> 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 < |
+ | '''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. |
+ | |||
+ | 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> |
||
==Twiddle case== |
==Twiddle case== |
||
− | With the following (for example, in [[vimrc]]), you can visually select text then press < |
+ | 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. |
<pre> |
<pre> |
||
Line 83: | Line 87: | ||
return result |
return result |
||
endfunction |
endfunction |
||
− | vnoremap ~ |
+ | vnoremap ~ y:call setreg('', TwiddleCase(@"), getregtype(''))<CR>gv""Pgv |
</pre> |
</pre> |
||
==References== |
==References== |
||
*{{help|case}} |
*{{help|case}} |
||
+ | *{{help|'tildeop'}} |
||
==Comments== |
==Comments== |
||
+ | The following will skip single-letter words and words that aren't in uppercase. It also accounts for non-english latin characters. |
||
+ | <pre> |
||
+ | :s/\v\C<([A-ZÀ-Ý])([A-ZÀ-Ý]+)>/\u\1\L\2/g |
||
+ | </pre> |
||
+ | --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) |
Revision as of 05:19, 27 August 2014
You can change the case of text:
- Toggle case "
HellO
" to "hELLo
" withg~
then a movement. - Uppercase "
HellO
" to "HELLO
" withgU
then a movement. - Lowercase "
HellO
" to "hello
" withgu
then a movement.
Alternatively, you can visually select text then press ~
to toggle case, or U
to convert to uppercase, or u
to convert to lowercase.
Examples
~
- Toggle case of the character under the cursor, or all visually-selected characters.
3~
- Toggle case of the next three characters.
g~3w
- Toggle case of the next three words.
g~iw
- Toggle case of the current word (inner word – cursor anywhere in word).
g~$
- Toggle case of all characters to end of line.
g~~
- Toggle case of the current line (same as
V~
).
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:
U
- Uppercase the visually-selected text.
- First press
v
orV
then move to select text. - If you don't select text, pressing
U
will undo all changes to the current line.
gUU
- Change the current line to uppercase (same as
VU
).
gUiw
- Change current word to uppercase.
u
- Lowercase the visually-selected text.
- If you don't select text, pressing
u
will undo the last change.
guu
- Change the current line to lowercase (same as
Vu
).
Title case
The :s
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):
:s/\<\(\w\)\(\w*\)\>/\u\1\L\2/g
Explanation The search pattern is \<\(\w\)\(\w*\)\>
which searches for \<
(beginning of word), then \w
(a word character), then \w*
(zero or more word characters), then \>
(end of word). The \(...\)
create subexpressions to be recalled with \1
and \2
in the replacement. The replacement is \u\1\L\2
which substitutes the two subexpressions transformed: The \u
converts the first character of what follows to uppercase, while \L
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 "I'll" or "she's". An alternative based on whitespace for word boundaries is:
:s/\<\(\w\)\(\S*\)/\u\1\L\2/g
Twiddle case
With the following (for example, in vimrc), you can visually select text then press ~
to convert the text to UPPER CASE
, then to lower case
, then to Title Case
. Keep pressing ~
until you get the case you want.
function! TwiddleCase(str) if a:str ==# toupper(a:str) let result = tolower(a:str) elseif a:str ==# tolower(a:str) let result = substitute(a:str,'\(\<\w\+\>\)', '\u\1', 'g') else let result = toupper(a:str) endif return result endfunction vnoremap ~ y:call setreg('', TwiddleCase(@"), getregtype(''))<CR>gv""Pgv
References
Comments
The following will skip single-letter words and words that aren't in uppercase. It also accounts for non-english latin characters.
:s/\v\C<([A-ZÀ-Ý])([A-ZÀ-Ý]+)>/\u\1\L\2/g
--Jenny 165.2.186.10 19:05, April 5, 2012 (UTC)
- Nice, thanks. I added
\C
to your command above to make the search case sensitive (it won't skip lowercase words if'ignorecase'
is set, unless\C
is present). JohnBeckett 09:58, April 6, 2012 (UTC)