Motions in Neovim Commands

We learned in the previous section that operators define the action that the command will take. Motions, on the other hand, define the direction or movement over which that action will take effect.

Some motions can take effect without an operator. For example, in normal mode the j and k motions move the cursor up and down. In general, however, an operator is required for the motion to take effect.

Motions come in two varieties:

Line-wise

Line-wise motions are those that effect an entire line. Line-wise motions are inclusive, meaning that they always include the lines that contain the start and end positions.

Character-wise

Character-wise motions operate on characters within a line. Examples of character-wise motions are the horizontal motions. Character-wise motions are further split into two sub-types:

Inclusive

Inclusive character motions are those that include the start and end positions in the text over which the operator operates.

Exclusive

Exclusive motions are those that exclude the end position (the character that is closest to the end of the buffer) from the text over which the operator operates.

Overriding

You can convert a line-wise motion to a character-wise motion, or vice-verse by inserting one of the following characters after the operator, and before the motion:

CommandAction
vforce operator to work charwise
Vforce operator to work linewise
C-Vforce operator to work blockwise

Additionally, if v is used with a character-wise motion, it changes inclusive motions to exclusive motions, and vice-versa.

Let's take a look at a few common commands using character-wise motions to see how they behave. Starting with the following buffer and cursor position:

Initial Conditions
I·have·always·believed,·and·I·still·believe,
that·whatever·good·or·bad·fortune·may·come·our·way
we·can·always·give·it·meaning·and
transform·it·into·something·of·value.
NORMAL
40%
2:9
 

Let's first take a look at an exclusive motion, such as w:

Select Text
vw
I·have·always·believed,·and·I·still·believe,
that·whatever·good·or·bad·fortune·may·come·our·way
we·can·always·give·it·meaning·and
transform·it·into·something·of·value.
VISUAL
40%
2:15
 
Delete text
dw
I·have·always·believed,·and·I·still·believe,
that·whagood·or·bad·fortune·may·come·our·way
we·can·always·give·it·meaning·and
transform·it·into·something·of·value.
NORMAL
40%
2:9
 
Delete with Forced Inclusive
dvw
I·have·always·believed,·and·I·still·believe,
that·whaood·or·bad·fortune·may·come·our·way
we·can·always·give·it·meaning·and
transform·it·into·something·of·value.
NORMAL
40%
2:9
 

In Step 1 we execute vw to highlight the text over which this motion will operate. In Step we first hit Esc to return to normal mode, then execute dw to delete a word. Note that although the g was highlighted in Step 1, it was not deleted. Now, lets :undo and repeat that command but forcing inclusive behavior by executing dvw. Comparing the results in Steps 2 and 3 shows that forcing inclusive behavior causes the g to be deleted.

Now let's look at an inclusive function, such as e:

Select Text
ve
I·have·always·believed,·and·I·still·believe,
that·whatever·good·or·bad·fortune·may·come·our·way
we·can·always·give·it·meaning·and
transform·it·into·something·of·value.
VISUAL
40%
2:13
 
Delete text
de
I·have·always·believed,·and·I·still·believe,
that·wha·good·or·bad·fortune·may·come·our·way
we·can·always·give·it·meaning·and
transform·it·into·something·of·value.
NORMAL
40%
2:9
 
Delete with Forced Exclusive
dve
I·have·always·believed,·and·I·still·believe,
that·whar·good·or·bad·fortune·may·come·our·way
we·can·always·give·it·meaning·and
transform·it·into·something·of·value.
NORMAL
40%
2:9
 

As in the previous example, in Step 1 we execute ve to highlight the text over which the motion will operate. In Step 2 we return to normal mode, then execute de to delete to the end of the word. Finally, in Step 3 we return to the starting point, then call this command with exclusive behavior. In this case, the last letter of the word (r) is omitted from the delete operation.