Learn Practice Daily golf Reference
Macros·Lesson 2 of 5·9 min·0/8 exercisesPro

Robust macros

Write cards that work on every line: start from a known place (0, I, A), move by what's always there (f, not 5x), end on the next line, and let a failing step stop a too-big count.

Scene

Monday, 1:30 p.m. The refunds export, after your quick 5x card: fine for 36 rows, then from row 37, where the ids go from four digits to five, every row keeps a comma it should have lost.

A card written on the first row only has to work for the first row. Yours has to work for all of them.

From memory

Warm-up from earlier chapters

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

Cook from the last card again.
press the keys…2 keys
1 of 2
Cook from the last card again.

Steps that are true everywhere

Write the card so it works on any order, not just the first: no "five steps along", only steps that are true on every line. Move by what's always there: df, deletes up to and including the comma, however long the id. f, t, $, I and A find their place on any line; 5x, 3l and 2w only on lines like the first.

Before
1003,veggie,1
10021,pollo,1
1022,tinga,2
qa5xjq
veggie,1
10021,pollo,1
1022,tinga,2
@a
veggie,1
,pollo,1
1022,tinga,2
Written with 5x: right on the first row, but on the five-digit id it leaves the comma behind.
Before
1003,veggie,1
10021,pollo,1
1022,tinga,2
qadf,jq
veggie,1
10021,pollo,1
1022,tinga,2
@a
veggie,1
pollo,1
1022,tinga,2
Written with df,: to the comma, wherever it is.
Try it

Ids of any length

Drop the id column from every row, with ids from three to six digits long. A card with df, and j, then a count.

Fix the marked spots · 6 left
1998,al-pastor,2
11003,veggie,1
21011,birria,3
310021,pollo,1
410045,carnitas,2
5100012,tinga,4
NORMALLoading the editor…0.0s1:1

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

Start at the stove

The card also starts wherever you happen to be. If the cursor is in the middle of the first row when you start writing, df, deletes from there, and j carries that column down to the next row. Make the first step put you somewhere known: 0 (or ^, I, A). Then it doesn't matter where you were.

Try it

From the middle

Same job, but the cursor starts in the middle of the first row. Begin the card with 0.

Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

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

A step that fails stops the cooking

When a step on the card can't be done (an f, on a line with no comma, a j on the last line), Vim stops cooking at once, count and all. So with a card that ends in j, 99@a on a list of 30 lines stops when j runs out of lines. It also means one odd line in the middle stops the whole run there.

Check yourself

Where does it stop?

1a,1
1b,2
2c
3d,4
NORMAL
Keys pressed: qa0f,xjq9@a

The card is 0 f, x j, written on the first line. Then 9@a. Which lines lose their comma?

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

Stopped halfway

A note sits in the middle of the refunds, with no comma. Write the card (qa 0 df, j q) and cook it with 99@a: it stops on the note, which is what you want. Then j past the note and 99@a again.

Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

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

A card that doesn't stop

A big count is safe only when the card fails exactly where the job ends. Some lines don't fail; they just get the wrong edit. A row someone already fixed by hand still has a comma, so df, happily takes its item name next. On a list like that, count exactly, or stop the run before the row.

Check yourself

Already done

11,a,1
122,b,2
2c,3
NORMAL
Keys pressed: qa0df,jq9@a

The last row was done by hand already. The card is 0 df, j, written on the first row, then 9@a. What does the list say?

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

Robust cards, from memory

Press the keys.

Write a card into jar a: from the start of the line, delete through the first comma, then down a line.
scroll here, then press the keys8 keys
1 of 3
Write a card into jar a: from the start of the line, delete through the first comma, then down a line.
Challenge

Four odd lists

Four refunds lists, and in each one something isn't like the first row. Look before you write the card: where will it stop, and is that where the job ends? Test it once on the second row, run it, and check the last line.

Stuck? Show a hint
Pro
The rest of this lesson is in Pro

The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.

See Pro

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

The refunds importFinish the exercise above to see it.