vimtricks.wiki Concise Vim tricks, one at a time.

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 operator
  • iw is 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
  • gUiw forces the word to UPPERCASE, guiw forces it to lowercase
  • gU and gu accept any motion: gUap uppercases 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~iw then w.w. to toggle successive words

Next

How do I edit multiple lines at once using multiple cursors in Vim?