: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.
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.
Warm-up from earlier chapters
Two from earlier chapters, from memory, before the new idea. Two wrong tries show the answer.
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.
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.
This exercise needs a keyboard. Read on, and play it at your computer.
| At the prompt | Means |
|---|---|
| y / n | replace this one / skip it, then ask about the next |
| a | replace this one and all the rest, no more questions |
| l | replace this one, then stop (the last) |
| q or Esc | stop 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.
Rider 12 took order 4401.Courier #12 took order 4401.(\d+) matched 12, and \1 puts it back in the new text.Numbers kept
Every "Rider N" in the docs becomes "Courier #N". One slip with a circled number and \1.
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.
The thing you found
You send :s/rider/[&]/↵. What does the line say?
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.
Back to capitals
The lines that start with courier should start with Courier. The one in the middle of a sentence stays. One slip.
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis 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.
{ order: 4401, eta: 12 },{ order: 4402, eta: 7 },{ order: 4401, eta: 720 },{ order: 4402, eta: 420 },Which number?
You send :s/\d\+/\=submatch(0)+1/↵. What does the line say?
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/↵.
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.
Small print, from memory
Press the keys.
order with ticket in the file, asking at each one.order with ticket in the file, asking at each one.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
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.