Suppose that we have the following text in normal mode (cursor is indicated by
hello world ^
If we use
dw, we delete
hello<Space>1 and only
world is left; if we
hello is deleted and
<Space>world is left. Have you ever wondered
dw do not delete
de will delete the
seems that motion
w are somewhat inconsistent in their behaviours. It
turns out that it has something to do with the exclusivity of motions in Vim.
Exclusive and inclusive motion explained
Most Vim motions are either exclusive or inclusive. This nature of a motion is
best revealed when you perform an operation (these operations include, but are
not limited to
y) followed by these motions.
Inclusive means that
the texts between the start and end position of the cursor is used for that
exclusive means that the last character near the end of
current buffer2 is not included in the operation.
If you had known the above difference between the exclusive and inclusive
motion, you would not be surprised about the so called inconsistency between
de. The reason is that
w is an exclusive motion and
e is an
inclusive motion. Someone has asked on the Stack Exchange site on why backward
db can not delete the character in the cursor
That is also because
b is an exclusive motion so that the last character
toward the end of the buffer is not included.
Change the exclusivity of a motion
To force an exclusive motion to be inclusive or vice versa, you can prepend
the motion by
v (from Vim doc):
v When used after an operator, before the motion command: Force the operator to work characterwise, also when the motion is linewise. If the motion was linewise, it will become |exclusive|. If the motion already was characterwise, toggle inclusive/exclusive. This can be used to make an exclusive motion inclusive and an inclusive motion exclusive.
Take the case in the first paragraph for an example,
dvw will delete
w becomes inclusive) and
dve will delete only
becomes exclusive). To find more detailed doc on this behaviour, read
:h exclusive carefully.
License CC BY-NC-ND 4.0