How to change letters in a word from upper case to lower case or the other way around (swap case)?

Vim

Vim Problem Overview


Instead of deleting the word and retyping all the letters once again in the opposite case, I'd like to find some smart way in Vim to solve the problem.

Vim Solutions


Solution 1 - Vim

It's

g~iw

with the cursor on the word.

Key:

  • g flag (I couldn't find a good reference for this...)
  • ~ toggle case; alternatively use U for to-upper or u for to-lower
  • iw selects the Inner Word, i.e. the word that the cursor is on; ip selects the Inner Paragraph

See Michael Jakl's Vim Introduction and Tutorial - concise and has some nice graphical explanations.

Solution 2 - Vim

g~ followed by a "motion" will flip the case of the letters.

gU will upper-case them

gu will lower case them

So

g~w will flip the case of the letters to the end of the current word.

guG will lower case the letters to the end of the file

gU$ will upper case the letters to the end of the current line.

Solution 3 - Vim

you can do this in normal mode: vEU (having the cursor at the beginning of the word or pressing b to move it there)

v - go to visual

E - go to end of the word

U - make the visual selection uppercase

Instead of the U you can do u for lowercase or ~ for case flip.

Solution 4 - Vim

~ (tilde) key. Should change the case of whatever is under the cursor. Works in normal and visual mode.

Solution 5 - Vim

You can select the word with visual mode (viw) and press ~, it switches case for all letters in the word.

Solution 6 - Vim

I would liek to emphasize that, in order to achiving switch the upper/lower case of a word, it depends on what is the exact position of the cursor.

if the cursor is now at the first character of the word, you can use g~w:

g stands for you want to do some operation in some scope(while the w definese the exact scope; ~ means you want to switch the upper&lower case(you can substitue ~ with U/u, which means you want to switch the word into upper case/lower case); w means you want to do the case switch from the cursor to the end of the word;

if the cursor is now in the middle of the word, not the very beginning of it, you can use g~iw:

iw means you want to do the case swith for the word in which the corsor is now located.

Solution 7 - Vim

Shift + F3 flips the word(s) between all uppercase, just the first letter and all lowercase. Has changed my life

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHaiyuan ZhangView Question on Stackoverflow
Solution 1 - Vimmartin claytonView Answer on Stackoverflow
Solution 2 - VimbmbView Answer on Stackoverflow
Solution 3 - VimfikovnikView Answer on Stackoverflow
Solution 4 - VimprevailrobView Answer on Stackoverflow
Solution 5 - VimMatteo RivaView Answer on Stackoverflow
Solution 6 - VimZhaoGangView Answer on Stackoverflow
Solution 7 - VimDanView Answer on Stackoverflow