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

Repeat substitutions

The kitchen keeps the last slip: :&&↵ sends it again on this line with its flags, g& to every line (with your last search as the pattern, so :%&&↵ after a search); and * then :%s//new/g renames the word you pointed at.

Scene

Thursday, 1:30 p.m. You write a careful slip for one line, :s/\<rider\>/courier/g, whole words only, so riders and riderCount wait for their own decisions. It works. Now the same order is wanted on a few lines further down, and then on every line of the file.

Writing the slip out again is how typos get in. The kitchen keeps a copy of the last one.

From memory

Warm-up from earlier chapters

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

Wrap every user on this line in brackets, using the match itself.
press the keys…14 keys
1 of 2
Wrap every user on this line in brackets, using the match itself.

The same slip again

:&&↵ sends the last slip again on the line you're on, small print and all: same pattern, same replacement, same g. g& sends it to every line in the file, with one catch: it takes its pattern from your last search. Straight after the slip that's the slip's own pattern (a :s counts as a search), so it does what you expect; search for something else in between and g& replaces that instead. (Plain & also repeats the slip on this line, but in Vim it drops the small print and changes only the first match; Neovim maps & to :&&↵, so there it keeps it. :&&↵ means the same everywhere.)

Before
rider = 1; rider++;
log(rider, rider);
free(rider, rider);
:s/rider/courier/g↵
courier = 1; courier++;
log(rider, rider);
free(rider, rider);
j:&&↵
courier = 1; courier++;
log(courier, courier);
free(rider, rider);
jg&
courier = 1; courier++;
log(courier, courier);
free(courier, courier);
The slip with g on line 1; :&&↵ sends it again on line 2, g and all; g& sends it to every line.
Try it

Here, and there

Rename rider on the first line with the whole-word slip (:s/\<rider\>/courier/g). Then the same on line 2 and line 4 with :&&↵, leaving line 3 alone.

1 of 2Change the red text to courier = nearestRider(riders); courier
1const rider = nearestRider(riders); ridercourier = nearestRider(riders); courier.busy = true;
1log("picked", rider.id); notify(rider.phone);
2const zone = zoneFor(order);
3return { rider, eta: rider.eta };
NORMALLoading the editor…1/20.0s1:1

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

Check yourself

Which pattern?

1a b a
1b a b
NORMAL
Keys pressed: :s/a/x/g↵/b↵g&

You send :s/a/x/g↵ on line 1, then search for b with /b↵, then press g&. What does the file say?

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

Every line

Rename on the first line with the whole-word slip, check it, then send it to the whole file with g&.

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

A search in between

You sent the whole-word slip on line 1, then searched for zone to check something. g& would now rename every zone. Rename rider on the rest of the file anyway: :%&&↵ is the slip again on every line (%) with its own pattern and small print. Typing the slip out again works too.

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.

Point first, then the slip

An empty pattern means the last thing you searched for. So put the cursor on a word, press * (it searches for exactly that whole word), then :%s//courier/g: no pattern to type, and riders and riderCount are safe because * only matches the whole word.

Try it

Point and rename

The cursor is on rider. *, then :%s//courier/g↵: the whole word only, everywhere.

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

Again, from memory

Press the keys.

Send it again on this line, flags and all.
scroll here, then press the keys4 keys
1 of 4
Send it again on this line, flags and all.