IN-Decent

Re-decentralizing internet with free software

Convert Case of Characters in Vim

Posted at — Aug 21, 2021

Converting case of characters to upper or lower case can be easily done in vim by using the following key combinations in normal mode.

  1. Toggle case of character under cursor - ~

  2. Toggle case of n characters from current char - n~

  3. Toggle case of n words from current char - g~nw

  4. Toggle case of current word(cursor anywhere in word)- g~iw

  5. Toggle case of all characters till end of line - g~$

  6. Toggle case of all characters in current line - g~~

To convert only to upper case one can substitute ~ with U in the above commands starting with g(commands 3-6).

Similarly to convert only to lower case one can substitute ~ with u in the above commands starting with g(commands 3-6).

Note:

If you use U without the g char as in first two key combinations listed above, vim will undo all change to the current line. Similarly for u will undo the last change made to file instead of lower casing the char.

References:

  1. Vim Tips Wiki - Switching case of characters