How do I toggle the case of an entire word in Vim?
Answer
g~iw
Explanation
The g~iw command toggles the case of every character in the word under the cursor — uppercase letters become lowercase and vice versa. This is faster than visually selecting the word and pressing ~ on each character.
How it works
g~is the toggle-case operatoriwis the "inner word" text object, selecting the word under the cursor- Together they flip the case of every character in the word
Example
Given the text with the cursor on Hello:
Hello World
Pressing g~iw produces:
hELLO World
Pressing g~iw again on the same word restores the original:
Hello World
Tips
- Use
g~$to toggle case from the cursor to the end of the line - Use
g~~to toggle case of the entire current line gUiwforces the word to UPPERCASE,guiwforces it to lowercasegUandguaccept any motion:gUapuppercases a paragraph,gu$lowercases to end of line- The
~key by itself toggles the case of the single character under the cursor and moves right - Pair with
.to repeat on the next word:g~iwthenw.w.to toggle successive words