Juan Valencia's Website

Uppercase and lowercase characters in Vim

In Vim, we can turn characters to uppercase, lowercase or invert the case in a few key presses.

Turn characters to Uppercase in Vim

We can do a visual selection of the text that we want to convert to uppercase and press an uppercase 'U'.
We can use gUU to transform a line to uppercase.
Or we can use motions such as gUw to turn a word into uppercase, gUt: to turn every character to uppercase from the cursor until a colon is found (it won't work if a colon is not further ahead in the line), and so on.
We can turn to uppercase a range of lines using:

:[range]g/^/normal gUU

e.g.:

:3,8g/^/normal gUU
:.,+10g/^/normal gUU

The first example would turn to uppercase from the line 3 to the line 8, the second example would turn to uppercase the current line and the ten next following lines (hence this command would turn to uppercase 11 lines). You can replace the ^ for a regular expression, in this case only the lines with a matching pattern would be converted to uppercase.

Turn characters to Lowercase in Vim

We can do a visual selection of the text that we want to convert to lowercase and press a lowercase 'u'.
We can use guu to turn an entire line to lowercase.
Or we can use motions such as guw to turn a word to lowercase, gut: to turn every character to lowercase from the cursor until a colon is found (it won't work if a colon is not further ahead in the line), and so on.
We can turn to lowercase a range of lines using:

:[range]g/^/normal guu

e.g.:

:3,8g/^/normal guu
:.,+10g/^/normal guu

The first example would turn to lowercase from the line 3 to the line 8, the second example would turn to lowercase the current line and the ten next following lines (hence this command would turn to lowercase 11 lines). You can replace the ^ for a regular expression, in this case only the lines with a matching pattern would be converted to lowercase.

Invert the case of characters in Vim

We can invert the case of the character under the cursor by pressing ~
We can do a visual selection of the text that we want to invert and press ~
We can use g~~ to invert the case of an entire line.
Or we can use motions such as g~w to invert the case of a word, g~t: to invert the case of every character from the cursor until a colon is found (it won't work if a colon is not further ahead in the line), and so on.
We can invert the case of a range of lines using:

:[range]g/^/normal g~~

e.g.:

:3,8g/^/normal g~~
:.,+10g/^/normal g~~

The first example would invert the case from the line 3 to the line 8, the second example would invert the case the current line and the ten next following lines (hence this command would invert the case of 11 lines). You can replace the ^ for a regular expression, in this case only the lines with a matching pattern would have the case of its characters inverted.