Words and Text Objects in Vim

August 10, 2016 / Vim, Mad Coding

What are text objects in Vim? Before I can answer that question, we first need to discuss words.

There are words, and then there are WORDS

Not even joking, there really are two distinct types of these things. To quote Vim’s help page on the subject (see :h word):

A word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, EOL). This can be changed with the ‘iskeyword’ option. An empty line is also considered to be a word.

A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a WORD.

Under those definitions, given the following example text to operate on:

what-is-up; “i can’t even”

Assuming a default iskeyword setting, the following are words:

what, -, is, -, up, ;, ", i, can, ', t, even, and "

By contrast, in the same passage, these are considered WORDS:

what-is-up;, "i, can't, and even"

WORDS are bounded strictly go by surrounding whitespace, whereas the delineation of words is configurable. In my Vim configuration, I actually set my personal iskeyword option to include $, %, &, #, -, ', and + as “word” characters.

(As an aside, I’m on the fence about setting ' as a “word” character. For prose, it makes sense; for programming, not so much.)

Word motions

We can perform motions over words in several useful ways. (Hint/foreshadowing: words are like text objects in this respect.)

In each example below, the caret ^ character indicates the cursor position to illustrate where it ends up after each motion.

  1. We move the cursor to the beginning of the next word using the w key. Likewise, we use W to move to the beginning of the next WORD.

    • w: forward to start of next word
    • W: forward to start of next WORD


    Making friends is easy. Waiting-for-Superman... waiting...
             ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    w(ord)         ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    w(ord)            ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    W(ORD)                  ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    w(ord)                         ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    W(ORD)                                          ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    w(ord)                                                 ^


  2. To move the cursor to the end of the next word or WORD, we use the e or E key.

    • e: end of word
    • E: END of WORD


    Making friends is easy. Waiting-for-Superman... waiting...
             ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    e(nd)        ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    e(nd)           ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    E(ND)                 ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    e(nd)                         ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    E(ND)                                         ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    e(nd)                                                 ^


  3. Finally, we use b and B to move to the start of a word or WORD.

    • b: back to start of word
    • B: BACK to start of WORD


    Making friends is easy. Waiting-for-Superman... waiting...
                                                          ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    b(ack)                                          ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    b(ack)                                      ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    B(ACK)                  ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    B(ACK)            ^
    
    Making friends is easy. Waiting-for-Superman... waiting...
    b(ack)         ^


Counting motions

For each type of motion, we can tell Vim to perform the motion a number of times by typing a count ahead of the motion command.

For example, 2w would move two words ahead, and 4b would move four words back, and so on. The implications of this are very powerful and counts play a major role in how we compose commands in Vim. More on this later.

Operators

Now that we’ve got some basic word motions down, let’s review some basic Vim operators that use motions.

One of the first things you learn about Vim is that it is modal. We’ve been focused on moving around in Normal mode, but now we’re going to look at editing some text in Insert mode.

If you’ve used Vim at a novice level, you probably know about the i and a operators to activate Insert mode before/after the cursor. The two basic operators I’m about to cover go one step beyond that. They are:

These operators require a motion, and can also take an optional count. Here are some usage examples (An plus + indicates where we’ve switched into Insert mode as a result of a change operator).


Making friends is easy. Waiting-for-Superman... waiting...
dB     ^
friends is easy. Waiting-for-Superman... waiting...
^

Making friends is easy. Waiting-for-Superman... waiting...
d4W    ^
Making waiting...
       ^

Making  is easy. Waiting-for-Superman... waiting...
cw     ^
Making  is easy. Waiting-for-Superman... waiting...
       ^
       +

Making friends is easy. Waiting-for-Superman... waiting...
c3w    ^
Making . Waiting-for-Superman... waiting...
       ^
       +