Learn Practice Daily golf Reference

How to change to uppercase or lowercase in Vim

In Normal mode, gU makes text uppercase and gu lowercase, followed by how much: gUiw for the word under the cursor, gUU or guu for the whole line. ~ flips the case of the one letter under the cursor.

The ninja flipping a row of two-sided letter tiles on a workbench so their capital sides face up, a gU keycap beside the row.

The style guide for the shop's ordering code says constants go in capitals, like TAX_RATE at the top of shop.js. The two below it were typed in lowercase. The letters are right, so there's no reason to type them again.

Before
const open_hour = 11;
gUiw
const OPEN_HOUR = 11;
From the middle of the name, gUiw puts the whole word in capitals, underscore and all
Try it here

Constants in capitals

Read gUiw as "uppercase inner word". The cursor is in open_hour.

Put the name in capitals with gUiwshop.js
1const TAX_RATE = 0.08;
2const open_hour = 11;
1const close_hour = 22;
NORMALLoading the editor…0.0sLn 2, Col :9
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

Change the case of a whole line

gU and gu work like d and c. What comes after says how much, so gUw changes up to the next word and gU$ everything to the end of the line. Say the verb twice and it takes the whole line from anywhere on it. gUU puts the line in capitals and guu puts it in lowercase.

g~ flips the case of every letter it covers, capitals to small and small to capitals. That makes g~~ the fix for a line somebody typed with Caps Lock on, where the first letter came out small and everything after it came out big.

Try it here

The specials board

One line on the board went up with Caps Lock on, and the last line should shout like the heading does. The cursor is on the Caps Lock line.

Flip the line back with g~~board.txt
2SPECIALS
1Carnitas with pickled onion
3eXTRA GUACAMOLE IS FREE TODAY
1cash only
NORMALLoading the editor…0.0sLn 3, Col :11
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

Capitalize one letter with ~

~ flips the case of the letter under the cursor and then moves one to the right, which makes it the quickest way to give a word its capital first letter. Press it again and it flips the next letter along. A count flips that many in a row, so 3~ does three. On a digit or a comma it changes nothing and still moves on.

Check yourself

Twice to be sure

You want the line to read Al pastor. The cursor is on the a, in Normal mode, and you press ~ twice to be sure. What does the line say now?

BeforeJust a picture: pick an answer below (1, 2, 3, 4)
1al pastor
NORMAL
Pick an answer (click it, or press its number), then watch the engine play the keys to check it.

Change case in Visual mode

When it's easier to see what you're changing, select it first. v starts a selection that you stretch with any move, and V selects whole lines. Then U puts the selection in capitals and u puts it in lowercase. ~ flips it.

Before
Ask about our salsa bar
VU
ASK ABOUT OUR SALSA BAR
V selects the line and U puts it in capitals, leaving you back in Normal mode

Capitalize every word in a file

For a whole file, the substitute command can do it. In its replacement, \u makes the next character a capital and \U makes everything after it capitals. \<. matches the first character of each word and & stands for whatever matched, so :%s/\<./\u&/g gives every word on every line a capital first letter. Vim counts an underscore as part of a word, so max_rush becomes Max_rush.

Try it here

Fillings for the board

The fillings came out of a spreadsheet in lowercase, and on the board each word gets a capital.

Type :%s/\<./\u&/g and press ↵fillings.txt
1al pastor
1carne asada
2pollo verde
NORMALLoading the editor…0.0sLn 1, Col :1
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

KeysChanges
gUiw / guiwthe word under the cursor, to capitals / to lowercase
g~iwthe word, every letter flipped
gUU / guu / g~~the whole line (gUgU, gugu and g~g~ work too)
3gUUthree lines, starting with this one
gUw / gU$up to the next word / to the end of the line
~ / 3~one letter / three letters, moving right as it goes
U / u / ~ in Visual modethe selection, to capitals / to lowercase / flipped
gg then gUGthe whole file to capitals (gg then guG for lowercase)
:%s/\<./\u&/ga capital at the start of every word in the file
:%s/.*/\U&/every line in capitals
:s/.*/\L\u&/the line in lowercase with a capital first letter