The first text objects we will review are some of the simplest and most common, words. Neovim
provides not one, but two definitions for words, and distinguishes between them using the notation
"word" and "WORD".
word
A sequence of letters, digits, and underscores, separated by either non-word characters or
whitespace.
WORD
A sequence of non-blank characters, separate by whitespace.
The differences between these two definitions can be clarified a bit with a simple example. Let's
take a look at how each text object would treat the following text:
one two three-four five/six
The words in this sentence are what we would expect from basic English usage:
one, two, three, four, five, and six
The WORDs in this sentence, on the other hand, are slightly different:
one, two, three-four, and five/six
Let's look a bit more at what these examples. First, we can see that both text objects treat
characters separated by whitespace the same. The difference is how they treat words that are
separated by characters other than whitespace. In these cases the wordstext object takes a
more conservative approach and considers anything other than "word-characters" to define word
boundaries, while WORDs take a more liberal approach and consider most types of characters to be
part of the word itself.
To further demonstrate these two text objects, let's start from the following buffer, and
visually-select a word and a WORD:
In particular note that executing viw in Step 1 selected text up to, but not including, the
apostrophe. On the other hand, executing viW in Step 2 selected the entire sequence of text,
from the start of the line to the space. The ability to choose between either word and WORD
provides additional flexibility to fine-tune which text is ultimately selected.
viw vs vaw
Let's now take a look at the difference between selecting inside and around wordtext
objects. The example below show the sequence of selecting inside the word with viw, hitting
Esc to return to normal mode, then selecting again using vaw.
The cursor starts in the middle of "known", then several things happened after executing viw.
First, the selection extended from the first to the last character of the word. Second, the cursor
moved to the last character of the word. As a result, after hitting Esc to return to
normal mode the cursor remains in its new position. Next, in Step 2 we execute vaw to
visually-select around the word, which behaves slightly differently than viw did previously.
In particular, although the selection starts from the first character of the word, as it has done
previously, it now extends until the character after the end of the word. In short, operating
around the wordtext object includes the trailing whitespace.
Extending the Selection by word
Continuing from the previous example, we have still have an active selection around the wordtext
object, as shown in the start condition below:
Now, suppose we want to extend the selection to include a few more words. We
we can extend the selection to the start of the next word by hitting w again, as shown in
Step 1, then repeated for several more words.
Extending the Selection by WORD
For comparison, let's cancel that selection then repeat that process, except this time we will
use W to extend the selection so that we extend by WORDtext objects.
Starting from the same position as the previous example, pressing W a single time in Step 1
has the same behavior as pressing w had; because the next word boundary is with whitespace the
word and WORDtext objects are the same. Hitting it again in Step 2, however, behaves somewhat
differently. In this case, the WORD jumped past the punctuation to the start of the next word,
then again in Step 3.
Having seen the basics of working with the simplest text objects, let's move on to learn about
sentencetext objects.