Practical Vim< /a> vit Practical Vim< /a> Note that the styling for a Visual selection is the... development I thought I knew Vim, but Practical" name="description"/>
Tải bản đầy đủ - 0 (trang)
Tip 51. Trace Your Selection with Precision Text Objects

Tip 51. Trace Your Selection with Precision Text Objects

Tải bản đầy đủ - 0trang

Trace Your Selection with Precision Text Objects

• 121

Vim understands the structure of these well-formed patterns, and it allows

us to operate on the regions of text that they delimit. Text objects define regions

of text by structure (see :h text-objects ). With only a couple of keystrokes, we

can use these to select or operate on a chunk of text.

Suppose that our cursor was positioned inside a set of curly braces and we

wanted to visually select the text inside the {}. We could do so by pressing

vi} :


Buffer Contents


var tpl = [




var tpl = [




var tpl = [




var tpl = [




var tpl = [




var tpl = [




var tpl = [



Normally when we use Visual mode, one end of the selection is anchored to

a particular character, while the other end of the selection is free to move.

When we use motions such as l , w , and f{char} , we can expand or contract

the selection by moving the free end of the visual range.

What’s happening here is different. When we press vi} , Vim initiates Visual

mode and then selects all of the characters contained by the {} braces. Where

the cursor is positioned to begin with doesn’t matter so long as it’s located

somewhere inside a block of curly braces when the i} text object is invoked.

We can expand the selection again using another text object. For example, a"

selects a range of characters delimited by double quotes. i> selects everything

inside a pair of angle brackets.

report erratum • discuss

Chapter 8. Navigate Inside Files with Motions

• 122

Vim’s text objects consist of two characters, the first of which is always either

i or a . In general, we can say that the text objects prefixed with i select inside

the delimiters, whereas those that are prefixed with a select everything

including the delimiters. As a mnemonic, think of i as inside and a as around

(or all).

Take another look at the example above, paying close attention to whether

the text object leads with i or a . In particular, note the difference between it

and at . Note, too, that in this example a] expands the selection to span

multiple lines.

A partial list of Vim’s built-in text objects is summarized in the following table.

In the interests of neatness, some duplicates have been omitted. For example,

i( and i) are equivalent to each other, and so too are a[ and a] . Use whichever style you find most comfortable.

Text Object


a) or ab

A pair of (parentheses)

i) or ib

Inside of (parentheses)

a} or aB

A pair of {braces}

i} or iB

Inside of {braces}


A pair of [brackets]


Inside of [brackets]


A pair of


Inside of


A pair of 'single quotes'


Inside of 'single quotes'


A pair of "double quotes"


Inside of "double quotes"


A pair of `backticks`


Inside of `backticks`


A pair of tags


Inside of tags

Table 12—Delimited Text Objects

report erratum • discuss

Trace Your Selection with Precision Text Objects

• 123

Performing Operations with Text Objects

Visual mode makes for a nice introduction to text objects because it’s easy

to see what’s happening. But text objects reveal their true power when we

use them in Operator-Pending mode.

Text objects are not motions themselves: we can’t use them to navigate around

the document. But we can use text objects in Visual mode and in OperatorPending mode. Remember this: whenever you see {motion} as part of the syntax

for a command, you can also use a text object. Common examples include

d{motion} , c{motion} , and y{motion} (see Table 2, Vim's Operator Commands, on

page 25, for more).

Let’s demonstrate using the c{motion} command, which deletes the specified

text and then switches to Insert mode (:h c ). We’ll use it to replace {url} with

a # symbol, and then again to replace {title} with a placeholder:


Buffer Contents



ci" #


cit click here

'click here'

We can read the ci" command as “change inside the double quotes.” The cit

command can be read as “change inside the tag.” We could just as easily use

the yit command to yank the text from inside the tag, or dit to delete it.


Each of these commands requires only three keystrokes, and yet they’re elegant in spite of this terseness. I would almost go so far as to say that they

are self-documenting. That’s because these commands follow the rules of

Vim’s simple grammar, which is covered in Tip 12, on page 24.

In Tip 49, on page 114, and Tip 50, on page 118, we learned a couple of tricks

that allow us to move the cursor with precision. Whether we’re using f{char}

to search for a single character or /target to search for several characters,

the pattern of usage is the same: we look for a suitable target, take aim, and

then fire. If we’re good, we’ll hit our target with a single move. These power

moves allow us to cover a lot of ground with little effort.

Text objects are the next level up. If the f{char} and /pattern commands

are like a flying kick to the head, then text objects are like a scissors kick

that strikes two targets with a single move, as Figure 3, Vim's power moves

can strike with deadly precision, on page 124 illustrates.

report erratum • discuss

Chapter 8. Navigate Inside Files with Motions

• 124

Figure 3—Vim’s power moves can strike with deadly precision

Tip 52

Delete Around, or Change Inside

Text objects usually come in pairs: one that acts inside the object and another

that acts around the object. In this tip, we’ll examine a typical use case for each

kind of text object.

Vim’s text objects fall into two categories: those that interact with pairs of

delimiters, such as i) , i" , and it , and those that interact with chunks of

text, such as words, sentences, and paragraphs. You can find a summary

of the latter in Table 13, Bounded Text Objects, on page 124.


Buffer Contents


Current word


Current word plus one space


Current WORD


Current WORD plus one space


Current sentence


Current sentence plus one space


Current paragraph


Current paragraph plus one blank line

Table 13—Bounded Text Objects

I’ve labeled the first category as delimited text objects because they begin and

end with matching symbols. Words, sentences, and paragraphs are defined

report erratum • discuss

Delete Around, or Change Inside

• 125

by boundaries, so I’ve labeled this category as bounded text objects (Vim’s

documentation calls them “block” and “non-block” objects, but I find that to

be an unhelpful distinction).

Let’s compare the iw and aw text objects. Using our mnemonic, we can think

of these as acting inside the word or around the word, respectively. But what

does that mean?

The iw text object interacts with everything from the first to the last character

of the current word. The aw text object does the same, but it extends the range

to include a whitespace character after or before the word, if one is present.

To see how Vim defines the boundaries of a word, refer to Tip 48, on page 112.

The distinction between iw and aw is subtle, and it’s not immediately obvious

why we need them both, so let’s look at a typical use case for each of them.

Suppose that we want to delete the word “excellent” from the following sentence. We can do it using the daw command:


Buffer Contents


Improve your writing by deleting excellent adjectives.


Improve your writing by deleting adjectives.

This deletes the word plus one space, giving a clean result. If we used diw

instead, then we’d end up with two adjacent spaces, which is probably not

what we want.

Now let’s suppose that we want to change the word to something else. This

time we’ll use the ciw command:


Buffer Contents


Improve your writing by deleting excellent adjectives.


Improve your writing by deleting most adjectives.

The ciw command deletes the word without trimming any whitespace and

then puts us into Insert mode. That’s just what we want. If we had used caw

instead, then we’d end up running words together to read “mostadjectives.”

That would be easy enough to mend, but it’s better still if we can avoid the

problem altogether.

As a general rule, we could say that the d{motion} command tends to work

well with aw , as , and ap , whereas the c{motion} command works better with

iw and similar.

report erratum • discuss

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tip 51. Trace Your Selection with Precision Text Objects

Tải bản đầy đủ ngay(0 tr)