Learn Practice Daily golf Reference
Ex & substitution·Lesson 1 of 7·8 min·0/8 exercisesPro

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↵.

Scene

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.

From memory

Warm-up from earlier chapters

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

Jump to the partner of this bracket.
press the keys…
1 of 2
Jump to the partner of this bracket.
Check yourself

Monday's slip

1al-pastor
1veggie
2birria
NORMAL
Keys pressed: qaI"EscA",Escq:2,$normal␣@a↵

You write card a on line 1 (quotes and a comma), then send :2,$normal␣@a↵. Which lines does it cook the card on?

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

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.

Before
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);
}
:.,+2d↵
export function ridersOnline(riders: Rider[]) {
return riders.filter((rider) => rider.online);
}
This line and the two below (.,+2), the order d: three comment lines gone in one slip.
Try it

Three dead lines

The three comment lines in ridersOnline are history. From the first of them, one slip: :.,+2d↵.

Fix the marked spots · 3 left
1export function ridersOnline(riders: Rider[]) {
1 // TODO(2019): riders without a phone
2 // were paged instead, see pageRider()
3 // (pageRider was deleted on Friday)
4 return riders.filter((rider) => rider.online);
5}
NORMALLoading the editor…0.0s1:1

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.

RangeWhich lines
.the line you're on
$the last line
%every line in the file
5,10lines 5 to 10
.,+3this line and the three below
2,$line 2 to the last
'a,'bfrom ribbon a to ribbon b (track 11)
'<,'>the highlighted lines (Vim writes it for you)
Check yourself

Which tables?

1a
2b
1c
2d
3e
4f
NORMAL
Keys pressed: :.,+2d↵

The cursor is on b. You send :.,+2d↵. What is left?

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

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.

Before
const rider = nearestRider(riders);
log("rider", rider.id);
:%s/rider/courier/g↵
const courier = nearestRider(couriers);
log("courier", courier.id);
Every line (%), every match on the line (g). Note nearestRider keeps its capital R: the match is exact.
Try it

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
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.

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 ↵.

Try it

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.)

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.

Try it

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).

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

Slips, from memory

Press the keys.

Delete this line and the one below it.
scroll here, then press the keys7 keys
1 of 4
Delete this line and the one below it.