For a few weeks months now I’m learnings how to use VIM text-objects. There is an extended help wth help text-objects in VIM. I’m trying to condense the VIM help in a smaller blog entry here.

What are a text-objects in vim? Text-objects are things like a ‘paragraph’ or the text between braces or something like a word. Text-objects can be used with the normal vim commands y, d and c. To make you really understand it, I can recommend using control-V to actually visually select your text object.

In vim’s text-objects documentation they talk about an important property. A text-object selection is either inner or not…

Some text-object selectors:

  • aw - a word, works on words
  • iw - inner word, works on words
  • ab - brace, works with text between ( and )
  • ib - inner brace, works with text between ( and )
  • ... - there are many more

Note: the i is used to denote inner selection, the a for normal ones.

Consider the following text:

(passed init=/sbin/bootchartd)

(this is some stuff from Ubuntu’s bootchart implementation).

Now an inner brace selection will be

passed init=/sbin/bootchartd

without the braces. An inner text-object selector will always select less than the normal block selection. The normal brace selection will select

(passed init=/sbin/bootchartd)

with the braces. You can check this for your self with the combo: control-V+a+b (normal selection) or control-V+i+b (inner selection). Note the selection works from anywhere inside the braces: it does not matter where the cursor is positioned. With dib you delete the inner brace text-object.

The word text-object works by letting you select only the word (= inner selection) or the word + whitespace (= normal selection). Thus, with the same sentence as above and the cursor positioned on the p of passed.

(passed init=/sbin/bootchartd)

control-V+i+w, selects

(passed init=/sbin/bootchartd)

In contrast with, control-V+a+w, which selects

(passed init=/sbin/bootchartd)

With the space after ‘passed’ also.

See vim’s online help for more info, but I hope this little text was helpful to you (It certainly helped me).