Learn Practice Daily golf Reference
Macros·Lesson 4 of 5·7 min·0/7 exercisesPro

Macros over ranges

Instead of a count, give the card a list of lines: :2,$normal␣@a↵ cooks it on a range, from a highlight : fills in the highlighted lines, and :g/pattern/normal␣@a↵ only on lines that match.

Scene

Monday, 3:30 p.m. The real export: 500 rows, and the accountant has typed TOTAL lines by hand in the middle of them, which must stay exactly as they are. A count would cook straight through them.

Instead of a count, hand the card over with a list of lines to cook it on.

From memory

Warm-up from earlier chapters

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

Put this whole line, without its line break, back into jar a.
press the keys…5 keys
1 of 2
Put this whole line, without its line break, back into jar a.

Cook it on these lines

:normal runs Normal-mode keys on every line of a range, starting each one at the beginning of its line. So :2,$normal␣@a↵ cooks card a once on every line from line 2 to the last: no count to guess, and no j needed on the card, because each line starts fresh.

PartWhat it says
:2,$which lines: from line 2 to the last (% would be every line)
normalwhat to do on each: go to the start of the line and press these Normal-mode keys
@athe keys: cook card a

Two ways to say how often

A count and a range divide the work differently. With 4@a, the card has to move itself on: it ends with j, and you count the lines. With a range, Vim moves to each line for you, so the card leaves out the j, and you only say which lines. (This is a preview: the Ex track does ranges properly.)

Before
al-pastor
veggie
birria
pollo
qaI"EscA",Escq
"al-pastor",
veggie
birria
pollo
:2,$normal @a↵
"al-pastor",
"veggie",
"birria",
"pollo",
Write the card on line 1 (without j), then cook it on lines 2 to the end.
Try it

Every line from 2

Quote every item with a comma after it. Write the card on the first line, then :2,$normal␣@a↵.

Fix the marked spots · 14 left
1al-pastor",
1veggie",
2birria",
3pollo",
4tinga",
5chorizo",
6barbacoa",
NORMALLoading the editor…0.0s1:1

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

Check yourself

Cooked twice

1a
1b
2c
NORMAL
Keys pressed: qaA;Escq:%normal␣@a↵

You write the card on line 1 (A ;), then cook it on the whole file with :%normal␣@a↵. What does line 1 say?

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

On a highlight

Highlight some lines and press :: Vim fills in '<,'>, which means the highlighted lines. Then normal @a and ↵ cooks the card on just those.

Try it

Just these

Only the three items get a ;, not the notes around them. Write the card on al-pastor, then j, highlight that line and the next with Vj, and :normal␣@a↵.

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.

Only the lines that match

:g/pattern/normal␣@a↵ cooks the card on every line that matches the pattern, and skips the rest. The export's order rows start with a digit and the TOTAL lines don't, so :g/^\d/normal @a touches only the orders. (Track 12 shows everything :g can do.)

Check yourself

Which lines?

12201,al-pastor
1TOTAL,3
22202,veggie
NORMAL
Keys pressed: :g/^\d/normal␣A;↵

Before you hand over the real card, a small test: :g/^\d/normal␣A;↵. ^\d means a digit at the start of the line. Which lines get a ;?

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

Around the totals

Turn the order rows into objects and leave the TOTAL lines alone. Write the card on the first row (no j), then :g/^\d/normal␣@a↵. The row you wrote it on no longer starts with a digit, so it isn't cooked twice.

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.

From memory

Lists for the kitchen, from memory

Press the keys.

Cook card a on every line from 2 to the end.
scroll here, then press the keys14 keys
1 of 3
Cook card a on every line from 2 to the end.