Learn Practice Daily golf Reference
Insert mode power·Lesson 2 of 4·6 min·0/7 exercisesPro

One Normal command

Ctrl-o lends you the remote for one press: one Normal-mode command, like zz, D or 0, and you're typing again where it left you.

Scene

Monday, 11:30 a.m. Second rehearsal. No typos stay on screen now, but the code you're typing creeps down to the bottom of the projector, and the people at the back can't see it. Every time, you leave Insert mode, press zz to bring the line to the middle, and go back in.

It's one Normal-mode command. You shouldn't have to put the typing down for it.

From memory

Warm-up from earlier chapters

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

Rub out the last character, with the Ctrl chord rather than Backspace.
press the keys…
1 of 2
Rub out the last character, with the Ctrl chord rather than Backspace.

Borrow the remote for one press

Ctrl-o in Insert mode hands you the remote for exactly one command: the keys after it are read as Normal mode, and when that command is done you're back in Insert mode, typing where it left you. (At the end of a line the command starts from the last character, and the typing resumes at the end.) Ctrl-ozz brings the line you're typing to the middle of the screen and you carry on.

10// legacy.js — Byte Burrito, 2019–present. Please don't add anything here.
9// (Everyone has added something here.)
8
7const TAX_RATE = 0.0875;
6const MAX_DISTANCE_KM = 8;
5const OPEN_HOUR = 11;
4const CLOSE_HOUR = 23;
3const RUSH_START = 12;
2const RUSH_END = 14;
1
11function isOpen(now) {
1 const hour = now.getHours();
2 return hour >= OPEN_HOUR && hour < CLOSE_HOUR;
3}
4
5function isLunchRush(now) {
6 const hour = now.getHours();
7 return hour >= RUSH_START && hour < RUSH_END;
8}
9
10function formatPrice(cents) {
11 const dollars = Math.floor(cents / 100);
12 const rest = String(cents % 100).padStart(2, "0");
13 return "$" + dollars + "." + rest;
14}
15
16function subtotal(items) {
17 let sum = 0;
18 for (const item of items) {
19 sum += item.price * item.qty;
20 }
21 return sum;
22}
23
24function taxFor(amount) {
25 return Math.round(amount * TAX_RATE);
26}
27
28function totalFor(order) {
29 const base = subtotal(order.items);
NORMALLoading the editor…11:1
Try it

Off the bottom

You're typing a new line at the very bottom of the file, where the back row can't see it. Ctrl-ozz to bring it to the middle, then finish the line: === "rush"; and Esc. (This page checks the line you typed, not where the screen ended up: watch the screen yourself.)

Type ·=== "rush"; where it's shown in green
60// legacy.js — Byte Burrito, 2019–present. Please don't add anything here.
59// (Everyone has added something here.)
58
57const TAX_RATE = 0.0875;
56const MAX_DISTANCE_KM = 8;
55const OPEN_HOUR = 11;
54const CLOSE_HOUR = 23;
53const RUSH_START = 12;
52const RUSH_END = 14;
51
50function isOpen(now) {
49 const hour = now.getHours();
48 return hour >= OPEN_HOUR && hour < CLOSE_HOUR;
47}
46
45function isLunchRush(now) {
44 const hour = now.getHours();
43 return hour >= RUSH_START && hour < RUSH_END;
42}
41
40function formatPrice(cents) {
39 const dollars = Math.floor(cents / 100);
38 const rest = String(cents % 100).padStart(2, "0");
37 return "$" + dollars + "." + rest;
36}
35
34function subtotal(items) {
33 let sum = 0;
32 for (const item of items) {
31 sum += item.price * item.qty;
30 }
29 return sum;
28}
27
26function taxFor(amount) {
25 return Math.round(amount * TAX_RATE);
24}
23
22function totalFor(order) {
21 const base = subtotal(order.items);
20 const tax = taxFor(base);
19 const tip = order.tip || 0;
18 return base + tax + tip;
17}
16
15// DEAD CODE: the fax ordering line was switched off in 2020
14function faxOrder(order) {
13 const sheet = renderFaxSheet(order);
12 faxMachine.send(sheet, "+1-555-0100");
11 log("fax sent", order.id);
10 return true;
9}
8
7function validateOrder(order) {
6 if (!order.items || order.items.length === 0) return "empty";
5 if (order.items.some((i) => i.qty <= 0)) return "bad quantity";
4 if (!order.address) return "no address";
3 if (!isOpen(new Date())) return "closed";
2 return null;
1}
61export const isRush = (order) => order.priority·=== "rush";
INSERTLoading the editor…0.0s61:48

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

Drop the rest of the line

Halfway through a line you realise everything after the cursor should go. Ctrl-oD deletes from the cursor to the end of the line and leaves you typing. Or the line you're on should be a comment: Ctrl-o0 jumps to the start of it, and you type // there.

Before
return total + legacyFee(order) + 0.5
iCtrl-oD
return total
;Esc
return total;
Typing after total: Ctrl-oD drops the rest of the line, and the ; goes straight in.
Try it

The rest goes

Each goal starts in Insert mode, mid-line. Everything after the cursor goes: Ctrl-oD, then finish the line as shown and Esc.

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

Where does the typing go?

1const total = price
NORMAL
Keys pressed: A␣+␣taxCtrl-o0//␣Esc

You type + tax at the end of the line, then Ctrl-o0, then // . What does the line say?

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

Comment it out

You finished typing each line, then decided it shouldn't run. Without leaving Insert mode: Ctrl-o0, // , Esc.

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.

When to just leave

Ctrl-o is for one command in the middle of typing. When you've finished typing and the rest is Normal-mode work, two deletes, a move, Esc is the better button: one press, and the whole remote is yours.

Try it

Done typing

Each goal starts in Insert mode, at the end of what you just typed. The rest is Normal-mode work: the debug lines go; the line you typed belongs above the other one.

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.

Rehearsal twoFinish the exercise above to see it.
From memory

One press, from memory

You're in Insert mode, mid-line. Press the keys.

Bring this line to the middle of the screen and keep typing.
scroll here, then press the keys3 keys
1 of 3
Bring this line to the middle of the screen and keep typing.