Learn Practice Daily golf Reference

How to replace a character in Vim

Put the cursor on the wrong character and press r, then the right one. ra makes it an a, and you stay in Normal mode. To type over several characters, press R, type, then Esc.

The ninja popping one dull wrong tile out of a row of letter tiles and pressing a fresh glowing one into the same slot, the tiles on either side untouched, an r keycap on the counter.

The specials board goes to the printer at four, and proofreading turns up Carnitqs and Churroz. Each is one wrong letter. You could delete it with x, press i, type the right one and press Esc, but r does all that in one go. It writes the next character you type over the one under the cursor, and you never leave Normal mode.

Before
Carnitqs burrito 9.50
ra
Carnitas burrito 9.50
r then a writes an a over the q, and the cursor stays on it
Try it here

Proofread the specials

The cursor is on the q of Carnitqs.

Make the q an a with raspecials.txt
1Byte Burrito specials
2Carnitqs burrito 9.50
1Churroz 3.00
NORMALLoading the editor…0.0sLn 2, Col :7
Open this text in the playground

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

Replace several characters with a count

A count in front of r replaces that many characters, each with the same new one. The happy-hour pollo drops from 9.75 to 9.00, and with the cursor on the 7, 2r0 writes a zero over both digits. The cursor ends on the last character it replaced. If the count asks for more characters than the line has left, Vim beeps and changes nothing.

Check yourself

Two zeros

The cursor is on the 7 of the price, in Normal mode. You press 2r0. What does the line say now?

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

Type over a stretch of text with R

When there's more than one character to write over, press R for Replace mode. The bottom line shows -- REPLACE --, and each character you type writes over the one under the cursor until you press Esc. Nothing after it moves, which is what you want for a time or a price in the middle of a line. Type past the end of the line and the rest is added on.

Winter hours start on Monday, and the shop will close at 20:30.

Try it here

Winter hours

The cursor is on the first 2 of the closing time.

Press R, type 20:30 over the old time, then press Eschours.txt
1Byte Burrito
2Open 11:00 to 22:00 daily
NORMALLoading the editor…0.0sLn 2, Col :15
Open this text in the playground

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

Went too far? In Replace mode ⌫ steps back and puts back the character you wrote over, one per press. Press it more times than you typed and it only moves the cursor left, leaving the text before it alone. Once you've pressed Esc, Backspace can't bring anything back, and u (undo) is the way to get the old text.

1Open 11:00 to 22:00 daily
NORMALLoading the editor…Ln 1, Col :15

Split a line in two with r and Enter

r takes any character, Enter included. r↵ (r, then Enter) replaces the character under the cursor with a line break, so on the space between two items typed onto one line it splits them in two keys. A count doesn't make more lines here. 3r↵ replaces three characters with a single line break.

Try it here

One drink per line

Someone typed both drinks on one line. The cursor is on the space before Agua.

Replace the space with a line break: r↵drinks.txt
1Drinks
2Horchata 2.50 Agua fresca 2.00
NORMALLoading the editor…0.0sLn 2, Col :14
Open this text in the playground

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

Delete, substitute or swap one character

x deletes the character under the cursor, and the rest of the line closes up behind it. s deletes it and puts you in Insert mode, so one character can become a whole word. Type it, then press Esc.

xp swaps two letters typed the wrong way round. x deletes the first one, and p (paste) puts it back after the letter now under the cursor. On the h of Nahcos, that makes Nachos.

Try it here

Last fixes before printing

Three more slips on the board. The cursor is on the extra u of Buurrito.

Delete the extra u with xspecials.txt
1Buurrito bowl 10.50
1Nahcos 4.00
2Quesadilla & salsa 8.00
NORMALLoading the editor…0.0sLn 1, Col :3
Open this text in the playground

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

KeysWhat they do
r and a characterReplace the character under the cursor and stay in Normal mode.
3rxReplace three characters with xxx. The cursor ends on the last one.
r↵Replace the character with a line break, splitting the line.
RReplace mode. Type over text until Esc. Past the end of the line, it adds text.
⌫ in Replace modeStep back and put back the character you typed over.
xDelete the character under the cursor. 3x deletes three.
sDelete the character under the cursor and start typing in its place. 2s deletes two.
xpSwap the character under the cursor with the one after it.
.Do the last replace again on the character under the cursor.
uUndo the last change, whichever key made it.