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 case
u– change to lowercase
U– 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 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 (
gUiw– Uppercase the entire word (works if you’re inside the word,
guap– Lowercase the entire paragraph (around paragraph,
g~)– Toggle case until the end of the sentence
gUi)– Uppercase everything inside the parentheses (
gui"– Lowercase everything inside the quotes
gUU– Uppercase the entire line
g~^– Toggle case back to the beginning of the line
gu$– 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.
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?