Learn Practice Daily golf Reference
Text objects·Lesson 4 of 8·9 min·0/9 exercisesPro

Brackets

Brackets nest like packaging: i( opens the layer around the cursor, 2i( the next one out; i[, i{ and i< are their own wrappings, and di{ empties a body.

Scene

Tuesday, 2:00 p.m. The price calculator throws on the new menu file. Its arguments were written for the old one: subtotal wants the order now, deliveryFee wants the zone, and everything is a call inside a call inside a call.

You'll be changing what's inside brackets all afternoon, often brackets inside other brackets.

From memory

Warm-up from earlier chapters

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

Change the inside of this template string.
press the keys…3 keys
1 of 2
Change the inside of this template string.

Layers of packaging

Brackets nest like a delivery: the burrito in its foil, in a paper bag, in the box. With the cursor anywhere in subtotal(order), i( opens the layer you're in and takes order; a( takes it with its brackets. A count goes further out: 2i( is the next pair out, everything withTax gets, and 3i( is everything inside round.

i(
const total = round(withTax(subtotal(order), rate), 2);
2i(
const total = round(withTax(subtotal(order), rate), 2);
3i(
const total = round(withTax(subtotal(order), rate), 2);
a(
const total = round(withTax(subtotal(order), rate), 2);
One cursor, three layers: each count opens the next pair of brackets out. The colours show which brackets pair up.
1export function priceFor(order: Order, zone: Zone) {
2 const base = subtotal(order.items, order.extras);
1 const fee = deliveryFee(zone.km, zone.surge);
2 const taxed = withTax(base + fee, TAX_RATE);
3 return round(taxed, 2);
4}
NORMALLoading the editor…2:33
Try it

New arguments

Three calls with arguments from the old file. ci( from anywhere between the brackets, and type the new ones.

1 of 3Delete the text marked red
1export function priceFor(order: Order, zone: Zone) {
1 const base = subtotal(order.items, order.extras);
2 const fee = deliveryFee(zone.km, zone.surge);
3 const taxed = withTax(base + fee, TAX_RATE);
4 return round(taxed, 2);
5}
NORMALLoading the editor…1/30.0s1:1

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

Check yourself

Two layers out

1const total = round(withTax(subtotal(order), rate), 2);
NORMAL
Keys pressed: d2i(

The cursor is in order. You press d2i(. What does the line become?

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

Different wrappings

Each kind of bracket is its own wrapping, and its own object: i[ for square brackets, i{ for braces, i< for angle brackets. i( ignores square brackets and braces completely. Two shortcuts: ib is the same as i( and iB is i{; Vim calls them a block and a Block.

i[
render({ tags: ["pork", "spicy"], max: limit(3) });
i{
render({ tags: ["pork", "spicy"], max: limit(3) });
i(
render({ tags: ["pork", "spicy"], max: limit(3) });
Same cursor, three wrappings: the list, the object, and the call around it all.
Try it

Mangled arguments

Arguments replaced by junk across the codebase. ci( and type them back.

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

Which layer?

Three short changes; the work is picking the layer. render gets item instead of the whole object it's handed, the tag list goes empty, and deliveryFee gets just zone. Each time, look at where the cursor landed and count the wrappings out from there.

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.

An empty box keeps its shape

Braces on their own lines work too. With the cursor anywhere in a function's body, di{ deletes every line between the braces and leaves the braces where they were: an empty box, ready for new contents. ci{ does the same and starts Insert mode on the empty line.

Before
export function legacyTotal(order: Order) {
const sum = order.items.reduce((s, i) => s + i.price, 0);
return Math.round(sum * 1.07 * 100) / 100;
}
di{
export function legacyTotal(order: Order) {
}
di{ from the last line of the body: the body goes, the box stays.
Try it

Empty the boxes

The old defaults object and the old total function both get new contents tomorrow. Empty each box with di{, from any line inside it.

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

Brackets, from memory

Press the keys. The cursor is between the brackets.

Change everything inside these round brackets.
scroll here, then press the keys3 keys
1 of 5
Change everything inside these round brackets.
Challenge

Arguments and strings

Ten wrong arguments and strings across the codebase, with the cursor wherever the last fix left it.

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.

Boss round

The quote function

2:45. quote must work before the 3 p.m. deploy. Top to bottom: the filter's argument is inStock now, the tag list goes empty, withTax takes order, zone now (everything it gets, subtotal(items), zone.tax, goes), and legacyQuote's body goes. Name each layer.

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.

Extra practice optional

The chapter is done. These rounds keep going as long as you like: stop whenever it stops being useful.

Endless

Brackets, endlessly

Endless mangled arguments and strings.

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.