Learn Practice Daily golf Reference
Ex & substitution·Lesson 2 of 7·11 min·0/9 exercisesPro

:s flags and specials

The slip's small print: g every match on the line, c ask at each one (y or n), i any case; in the replacement & is the match, \1 a circled group, \u a capital, \= works it out. Then five files, five decisions.

Scene

Thursday, 11:00 a.m. The trial run of :%s/rider/courier/g on a copy: the code is right, but it also changed a link to the wiki, /riders/handbook, which still lives at that address. And in the docs, "Rider 12" should become "Courier #12", with the number kept.

Time to read the small print.

From memory

Warm-up from earlier chapters

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

Delete the last line of the file.
press the keys…4 keys
1 of 2
Delete the last line of the file.

The small print

After the last slash of :s go the flags, the small print on the slip. g you know: every match on the line, not just the first. c means ask me each time: Vim stops at every match, highlights it and waits for y (yes, replace this one) or n (no, skip it). And i ignores case, so rider also finds Rider and RIDER.

1const rider = nearestRider(riders, order.address);
1// docs: https://wiki.byteburrito.test/riders/handbook
2log("rider assigned", rider.id);
NORMALLoading the editor…1:1
Try it

Not the wiki

Rename every rider in code, but not in the wiki link on line 2. :%s/rider/courier/gc↵, then answer each question: y for code, n for the link.

Fix the marked spots · 2 left
1const rider = nearestRider(riderscourier = nearestRider(couriers, order.address);
1// docs: https://wiki.byteburrito.test/riders/handbook
2log("rider assigned", ridercourier assigned", courier.id);
NORMALLoading the editor…0.0s1:1

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

At the promptMeans
y / nreplace this one / skip it, then ask about the next
areplace this one and all the rest, no more questions
lreplace this one, then stop (the last)
q or Escstop here; what's done stays done

Point back at what you found

The replacement has its own small print. & means the whole thing you found. Parentheses in the pattern circle a part of it (in very magic mode, \v, from the search track), and \1 in the replacement means the first circled part: :%s/\vRider (\d+)/Courier #\1/g keeps each number.

Before
Rider 12 took order 4401.
:s/\vRider (\d+)/Courier #\1/↵
Courier #12 took order 4401.
The circled (\d+) matched 12, and \1 puts it back in the new text.
Try it

Numbers kept

Every "Rider N" in the docs becomes "Courier #N". One slip with a circled number and \1.

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.

Check yourself

The thing you found

1log(rider)
NORMAL
Keys pressed: :s/rider/[&]/↵

You send :s/rider/[&]/↵. What does the line say?

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

Capitals

\u makes the next letter a capital, and \U the rest of the replacement. After a careless rename, lines of the docs that start with a lowercase courier need it back up: :%s/^courier/\u&/, where ^ means at the start of the line and \u& is what you found, with a capital.

Try it

Back to capitals

The lines that start with courier should start with Courier. The one in the middle of a sentence stays. One slip.

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.

Work it out

One more line of small print, for numbers. \= at the start of the replacement means work it out: what follows is a calculation, done for each match, and submatch(0) is the text that matched. The courier app wants ETAs in seconds: :%s/eta: \zs\d\+/\=submatch(0)*60/ finds each ETA's number (\zs starts the match at the number, from the search track, so the order numbers are safe) and puts back that number times 60.

Before
{ order: 4401, eta: 12 },
{ order: 4402, eta: 7 },
:%s/eta: \zs\d\+/\=submatch(0)*60/↵
{ order: 4401, eta: 720 },
{ order: 4402, eta: 420 },
Each matched number, worked out again: 12 minutes is 720 seconds, 7 is 420. The order numbers didn't match, so they stay.
Check yourself

Which number?

1order 9 of 10
NORMAL
Keys pressed: :s/\d\+/\=submatch(0)+1/↵

You send :s/\d\+/\=submatch(0)+1/↵. What does the line say?

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

Minutes to seconds

Every eta becomes seconds; the order numbers stay. One slip that works the new number out: :%s/eta:␣\zs\d\+/\=submatch(0)*60/↵.

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

Small print, from memory

Press the keys.

Replace every order with ticket in the file, asking at each one.
scroll here, then press the keys20 keys
1 of 4
Replace every order with ticket in the file, asking at each one.
Challenge

Five files, five decisions

Five small files, each with its own catch: how far the rename reaches, a link to keep, a number to keep, capitals, and case. The line under the editor says what each one wants. Any slip that leaves the right text counts.

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.