Reputation: 4050
Given:
for (int i = 0; i < 10; i++){
_ <---Cursor position
3w
leads to
for (int i = 0; i < 10; i++){
_ <---Cursor position
and d3w
leads to
fori = 0; i < 10; i++){
_ <---Cursor position
i.e., even though motion 3w
takes cursor upto i
, i
itself is not deleted.
On the other hand, given:
for (int i = 0; i < 10; i++){
_ <---Cursor position
%
leads to
for (int i = 0; i < 10; i++){
_ <---Cursor position
and d%
leads to
for{
_ <---Cursor position
i.e., motion %
takes cursor upto )
and )
itself is deleted.
So, why is there two different effects of d{motion}
? Is there any single general rule of which both of these are consistent special cases?
Upvotes: 1
Views: 136
Reputation: 8898
Yes, there's a logic to that. In Vim, some motions such as w
are "exclusive", while other motions such as %
are "inclusive". This will determine whether the action will affect the last character of the motion or not.
You can actually override the "exclusive" or "inclusive" status of a motion by using the v
operator (note that v
is being used as an operator here, not starting Visual mode as it does when used as a Normal mode command!) So dv3w
(or d3vw
) will delete up to the beginning of third word "inclusive" of the character it lands on, while dv%
will delete up to the next matching bracket "exclusive".
In a way, Visual mode is somewhat similar, since a Visual selection is "inclusive" by default, so v3wd
would behave similarly to dv3w
. (Though this can be overridden by the 'selection'
option.)
See:
:help w
:help %
:help exclusive
(same as :help inclusive
):help o_v
:help 'selection'
Upvotes: 4