Ranges
Monday's :2,$normal @a was an order slip: which lines (., $, %, .,+3, ribbons, a highlight), what to do, and ↵ sends it: :.,+2d↵, :%s/rider/courier/g↵.
Thursday, 10:15 a.m. The all-hands: Byte Burrito's riders are couriers now. Every rider in the code, the scripts and the docs changes tonight, hundreds of them, and the app must not break.
You won't do that one word at a time. You've already given one order for many lines at once, on Monday, with a recipe card.
Warm-up from earlier chapters
Two from earlier chapters, from memory, before the new idea. Two wrong tries show the answer.
Monday's slip
You write card a on line 1 (quotes and a comma), then send :2,$normal␣@a↵. Which lines does it cook the card on?
An order slip
That was an Ex command, and it has the shape of an order slip for the kitchen. : opens the slip at the bottom of the editor; first you say which lines it's for (the range: 2,$), then the order itself (the command: normal @a), and ↵ sends it. The kitchen does it on every line in the range, as if each line were a table on the slip. Orders don't have to be a card: :.,+2d reads these three lines: delete.
export function ridersOnline(riders: Rider[]) { // TODO(2019): riders without a phone // were paged instead, see pageRider() // (pageRider was deleted on Friday) return riders.filter((rider) => rider.online);}export function ridersOnline(riders: Rider[]) { return riders.filter((rider) => rider.online);}.,+2), the order d: three comment lines gone in one slip.Three dead lines
The three comment lines in ridersOnline are history. From the first of them, one slip: :.,+2d↵.
This exercise needs a keyboard. Read on, and play it at your computer.
Which lines
The range is the top of the slip. Two addresses with a comma between them mean from here to there, both ends included; one address on its own is just that line. + and - count from the line you're on, like the gutter's ruler.
| Range | Which lines |
|---|---|
. | the line you're on |
$ | the last line |
% | every line in the file |
5,10 | lines 5 to 10 |
.,+3 | this line and the three below |
2,$ | line 2 to the last |
'a,'b | from ribbon a to ribbon b (track 11) |
'<,'> | the highlighted lines (Vim writes it for you) |
Which tables?
The cursor is on b. You send :.,+2d↵. What is left?
The replace order
The order you'll send most is s, substitute: :s/rider/courier/ replaces the first rider on each line in the range; add g at the end for every one on the line. So :%s/rider/courier/g is the whole rename in one slip, and :.,+2s/rider/courier/g does it in just three lines. The next lesson is all about s.
const rider = nearestRider(riders);log("rider", rider.id);const courier = nearestRider(couriers);log("courier", courier.id);%), every match on the line (g). Note nearestRider keeps its capital R: the match is exact.One function only
Tonight starts small: rename every rider inside riderReport only, all three lines of it, and nothing outside it. You choose the range.
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.
Ribbons and highlights as ranges
Last night's ribbons make a range too: :'a,'bd deletes from ribbon a to ribbon b, wherever they are, so you never need the line numbers. And if you highlight some lines and press :, Vim writes '<,'> on the slip for you: the highlighted lines. Then the command, and ↵.
Between two ribbons
Now assignRider, and only assignRider. The cursor is on its first line: leave ribbon ma there, ribbon mb on its closing brace, and send one slip for the lines between: :'a,'bs/rider/courier/g↵. (A highlight and : work just as well.)
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.
Whole-file renames
One word renamed through a whole file each time. One slip each: :%s/old/new/g↵, with \< and \> around the word when it also hides inside longer ones (track 5).
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.
Slips, from memory
Press the keys.