Vim Recipes Editing Changing the Case of Text

Changing the Case of Text

Problem

You want to change the case of a character or block of text. For example, you may want to change bob to Bob.

Solution

~
Toggles the case of the current character in Normal mode, or the selection in Visual mode.
u
Lowercases highlighted text. (Note: This only works in Visual mode; otherwise u will undo your last change).
U
Uppercases highlighted text. (Note: This only works in Visual mode; otherwise U will undo the changes made on the current line).

Discussion

As normal, these commands accept motions. For example:

VU
Uppercase current line.
guw
Lowercases current word.

To convert a string to title case, i.e. initial capitals, you can use the following regular expression: s/\<\(\w\)\(\w*\)\>/\u\1\L\2/g. Select the text you want to convert, hit :, then enter the regular expression. If you use this regularly, consider remapping a key to execute this command. For example:

nnoremap <F7> :s/\<\(\w\)\(\w*\)\>/\u\1\L\2/g<CR>
vnoremap <F7> :s/\%V\(\w\)\(\w*\)\%V/\u\1\L\2/g<CR>

Alternatively, you can install the titlecase plugin.