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

:g and :v

A slip that picks its lines by what they say: :g/pattern/ every line that matches, :v/pattern/ every line that doesn't, then any order: :g/console\.log/d↵, :v/courier/d↵, m$ (move below the last line).

Scene

Thursday, 3:00 p.m. The cleanup list before tonight's release: every console.log line goes, the TODO(rider) notes move to the bottom of their file so someone deals with them, and from the dispatch log only the courier lines are wanted.

None of those lines have numbers you know. What they have in common is what they say.

From memory

Warm-up from earlier chapters

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

Send it to every line in the file, straight after the slip.
press the keys…2 keys
1 of 2
Send it to every line in the file, straight after the slip.

Every line that says…

:g/pattern/ names the lines for the slip by what's on them: every line that matches. Then the order, carried out on each of them in turn: :g/console\.log/d deletes every line with a console.log on it. (The backslash makes the dot a real dot, from the search track.)

Before
const c = pick(order);
console.log("here", c);
assign(order, c);
console.log("done");
:g/console\.log/d↵
const c = pick(order);
assign(order, c);
Every line that matches, the order d: the two log lines gone, wherever they were.
Try it

Purge the debugging

Debugging lines left in three files. One :g slip for each.

1 of 3Fix the marked spots · 2 left
1export const config = {
1 apiUrl: "https://api.byteburrito.dev",
2 currency: "USD",
3 taxRate: 0.0875,
4 openHour: 11,
5 debugger;
6 closeHour: 23,
7 slogan: "Code by day, burritos by night",
8 debugger;
9};
NORMALLoading the editor…1/30.0s1:1

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

…and every line that doesn't

:v/pattern/ turns it round: every line that does **not** match (:g! means the same). From a log, :v/courier/d deletes everything that isn't about couriers, which leaves only the lines you wanted.

Before
09:58 rider 12 online
10:02 kitchen ticket 4401
10:03 courier 7 picked 4401
10:05 payment ok 4401
:v/courier/d↵
10:03 courier 7 picked 4401
Every line without courier, deleted: what's left is the courier lines.
Try it

Only the couriers

Keep only the lines of the dispatch log that mention a courier. 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.

Check yourself

What survives?

110:03 courier 7 picked 4401
110:05 payment ok 4401
210:12 Courier 31 online
NORMAL
Keys pressed: :v/courier/d↵

You send :v/courier/d↵ on these three lines. What is left?

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

Every empty line

1a
1
2b
3
4
5c
NORMAL
Keys pressed: :g/^$/d↵

You send :g/^$/d↵. What is left?

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

Any order at all

After :g/pattern/ can come any order on the slip. m$ is move below the last line: m for move, $ for the last line, as in a range (two chapters on, :m gets a chapter of its own). So :g/TODO(rider)/m$ moves each matching line to the bottom of the file, one after another, which keeps their sequence. And :g/pattern/normal … runs Normal-mode keys on each, as you did with a recipe card on Monday.

Try it

Notes to the bottom

Move both TODO(rider) notes to the bottom of the file, keeping their sequence. 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.

From memory

Every line that…, from memory

Press the keys.

Delete every line that mentions order.
scroll here, then press the keys11 keys
1 of 3
Delete every line that mentions order.
Challenge

Cleanup

Five files, and the line under the editor says what each one wants: lines to delete, lines to keep, lines to move, gaps to close, and a rename on some lines only. Say "every line that…" to yourself first; 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.