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.
fdsfsdfsdfasdf
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.
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)