Learn Practice Daily golf Reference
The Vim Way·Lesson 6 of 7·9 min·0/8 exercises

Undo chunks & counted arithmetic

u and Ctrl-r skip back and forward a whole change at a time. Ctrl-a and Ctrl-x click numbers up and down like a tally counter, and gCtrl-a numbers a column.

Scene

4:53 p.m. The order form is open, and every quantity on it is off by a few. From the next room comes the steady tap-tap-tap of someone holding down u, watching an hour of work disappear one chapter at a time.

Two things to learn before six: how far back u really goes, and how to change a number without retyping it.

From memory

Warm-up from earlier chapters

Two from earlier chapters, from memory, before the new idea. Two wrong tries show the answer.

That last change was a mistake. Take it back.
press the keys…
1 of 2
That last change was a mistake. Take it back.

Undo skips whole chapters

On the remote, u doesn't rewind frame by frame: it skips back a whole chapter. Each change is one chapter, and everything you type between i and Esc is a single chapter, however long it was. Three short inserts are three chapters; one long one is one.

1const order = placeOrder(cart);
NORMALLoading the editor…1:15
1const order = placeOrder(cart);
NORMALLoading the editor…1:15

Skip forward again

Skipped back too far? Ctrl-r skips forward a chapter: redo. (Last lesson paired u with .; on the remote, u's real mirror is Ctrl-r.) It only replays chapters you skipped back over: make a new change and the chapters ahead are out of its reach. (Moving around is safe; only a change does this.) That's the danger in the next room: one stray edit and that hour is beyond Ctrl-r.

1const tax = rate * total;
NORMALLoading the editor…1:7
Try it

Back and forth

Three steps, one file. Delete the two stray letters with x. Then undo the last deletion with u, and redo it with Ctrl-r.

1 of 3Change the red text to qty = order.items
1const qtyy = order.itemsx.length;
NORMALLoading the editor…1/30.0s1:7

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

Next doorFinish the exercise above to see it.

A tally counter

That's undo done. Now the quantities. Ctrl-a is a tally counter, like the clicker at the door that counts people in and out: each press clicks the number up by one, and Ctrl-x clicks it down. A count clicks several times at once, so 5Ctrl-a adds five. No deleting digits, no typing, and no off-by-one.

Before
qty: 3,
maxPerOrder: 12,
5Ctrl-a
qty: 8,
maxPerOrder: 12,
j2Ctrl-x
qty: 8,
maxPerOrder: 10,
5Ctrl-a clicks the 3 up five times; 2Ctrl-x clicks the next line down two.

It clicks the next number along

You don't have to be on the number. Ctrl-a clicks the first number at or after the cursor on the line, so from the start of qty: 3, it finds the 3 by itself. Only that one number changes; the rest of the line is left alone.

Check yourself

Which counter?

1price: 9, qty: 2
NORMAL
Keys pressed: 3Ctrl-a

The cursor is at the start of the line. You press 3Ctrl-a. What does the line say?

Pick an answer (or press its number), then watch the engine prove it.
Try it

Fix the order form

Each quantity is off by a few. Read the right number from the goal, then click it there with a count and Ctrl-a or Ctrl-x.

1 of 4Change the red text to 11
1export const config = {
1 apiUrl: "https://api.byteburrito.dev",
2 currency: "USD",
3 taxRate: 0.0875,
4 openHour: 5,
5 closeHour: 23,
6 slogan: "Code by day, burritos by night",
7};
NORMALLoading the editor…1/40.0s1:1

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

A row of counters

Select several lines and gCtrl-a clicks each line's first number once more than the line above: the first gets +1, the second +2, and so on. A column of zeros becomes 1, 2, 3, 4. Take the selecting as a recipe for now: V highlights the line you're on, each j stretches the highlight down a line, and gCtrl-a works on what's highlighted. Track 7 is all about selecting.

1export const TICKETS = [
2 { id: 0, item: "al-pastor" },
1 { id: 0, item: "veggie" },
2 { id: 0, item: "carnitas" },
3 { id: 0, item: "birria" },
4];
NORMALLoading the editor…2:3
Try it

Number the tickets

Every kitchen ticket says id: 0. Give them 1 to 4 in one go: V, j down to the last one, gCtrl-a.

Fix the marked spots · 4 left
1export const TICKETS = [
2 { id: 0, item: "al-pastor" },
1 { id: 0, item: "veggie" },
2 { id: 0, item: "carnitas" },
3 { id: 0, item: "birria" },
4];
NORMALLoading the editor…0.0s2:3

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

From memory

Chapters and clicks

From memory.

Take back the last change.
scroll here, then press the keys
1 of 4
Take back the last change.
Challenge

Inventory count

Quantities all over the codebase, each a few off. Click every one back to the right number with a count.

1 of 8Change the red text to 10
1export const menu = [
1 { id: "al-pastor", name: "Al Pastor", price: 9.5, spicy: true },
2 { id: "veggie", name: "Garden Veggie", price: 8.25, spicy: false },
3 { id: "carnitas", name: "Carnitas", price: 5, spicy: false },
4];
5
6export function findItem(id: string) {
7 const item = menu.find((m) => m.id === id);
8 if (!item) throw new Error("not on the menu: " + id);
9 return item;
10}
NORMALLoading the editor…1/80.0s1:1

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

Boss round

Closing stock

The day's deliveries, in inventory.ts: 60 more tortillas, a crate of 12 for each of the four lines below it, then 5 churros sold. The four crates are the same change four times; one tip: . remembers a count too.

Stuck? Show a hinttortillas: 60<C-a> · the next four: 12<C-a>, then j. j. j. · churros: 5<C-x>
1 of 3Change the red text to 20
1export const INVENTORY = {
1 tortillas: 140,
2 avocados: 36,
3 tomatoes: 52,
4 cilantro: 18,
5 lemonade: 24,
6 churros: 30,
7};
NORMALLoading the editor…1/30.0s1:1

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

Extra practice optional

The chapter is done. These rounds keep going as long as you like: stop whenever it stops being useful.

Endless

Stocktake

Endless quantities to click back into place, a few at a time.

Level 1Change the red text to 10
1export const menu = [
1 { id: "al-pastor", name: "Al Pastor", price: 9.5, spicy: true },
2 { id: "veggie", name: "Garden Veggie", price: 8.25, spicy: false },
3 { id: "carnitas", name: "Carnitas", price: 3, spicy: false },
4];
5
6export function findItem(id: string) {
7 const item = menu.find((m) => m.id === id);
8 if (!item) throw new Error("not on the menu: " + id);
9 return item;
10}
NORMALLoading the editor…lv 1 · 0 done0.0s1:1

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