Change character case
Vim has a simple way to change case. The easiest one to remember is ~
which just toggles the case of the current character to the opposite. But by learning how to combine Vim’s case-changing commands with movements, you can more efficiently swap the case of text. Here are Vim’s three case-changing commands:
~
– toggle the caseu
– change to lowercaseU
– change to uppercase
The real power of these commands comes with combining them with motions. You can use all three of them with motions and text objects, some examples of which I’ll detail below. Note that the ~
will work on its own because it will simply toggle the case of the character under the cursor. But u
and U
will not, as they are undo commands. Those two must be combined or simply used on a visual selection.
Here’s the pattern you can follow to combine these commands:
g + case-change (~/u/U) + motion
Check out some examples and it will become more clear:
gUw
– Uppercase until the end of the word (w
for word)gUiw
– Uppercase the entire word (works if you’re inside the word,iw
)guap
– Lowercase the entire paragraph (around paragraph,ap
)g~)
– Toggle case until the end of the sentencegUi)
– Uppercase everything inside the parentheses (i
for inside)gui"
– Lowercase everything inside the quotesgUU
– Uppercase the entire lineg~^
– Toggle case back to the beginning of the linegu$
– Lowercase to the end of the line
There are countless more variations you can make. I demonstrated a few of these in the video below. Check out :help text-object
and :help motion
for more examples. You might also want to read our post about using coercing text between various casing standards: TitleCase to camelCase, under_score to PascalCase, and more.