Reputation: 20648
Open Vim, and insert only the following line of text in the buffer.
hello world
In other words, press i, type hello world
and press Esc.
Press 0 to position the cursor at the first character of first line.
o
.Press de. You'll see that the characters from h
to o
have been deleted. Only the following text is left.
world
Open Vim, and insert only the following line of text in the buffer.
hello world
In other words, press i, type hello world
and press Esc.
Press 0 to position the cursor at the first character of first line.
w
.Press dw. You'll see that the characters from h
to have been deleted. Only the following text is left.
world
However, I was expecting everything from h
to w
to be deleted and only the following text to be left.
orld
First let me quote :help d
below.
*d*
["x]d{motion} Delete text that {motion} moves over [into register
x]. See below for exceptions.
In experiment 1, the motion due to e moved over from h
to o
and sure enough everything from h
to o
(including h
and o
) was deleted.
In experiment 2, the motion due to w moved over from h
to w
but everything from h
to w
(including h
and w
) was not deleted. Why?
The behaviour of dw, de, and db is summarized below.
Command Deletes character under the Deletes character under the
initial cursor position? final cursor position?
------- --------------------------- ---------------------------
dw Yes No
de Yes Yes
db No Yes
Why is the behaviour of the three commands inconsistent?
Upvotes: 31
Views: 1535
Reputation: 28399
According to the Vim documentation, you can use v
after the operator toggle the exclusive of inclusive nature of the characterwise motion:
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 above hello world
text as an example (|
indicate cursor position):
|hello world
vw
will turn w
motion to inclusive so that after dvw
, the text becomes:
orld
ve
will turn e
motion into exclusive so that after dve
, the text becomes:
o world
Upvotes: 1
Reputation: 196606
de
cuts everything from, and including, the character under the cursor up to, and including, the last character of the world, e
is an inclusive motion.
dw
cuts everything from, and including, the character under the cursor up to, and excluding, the next word, w
is an exclusive motion.
The answer to your question is not in :help d
(de
and dw
are perfectly consistent with it) but in :help e
and :help w
(e
and w
don't have to work the same because, as the doc says, one is inclusive and the other exclusive).
Always keep in mind that everything in Vim is about composability: de
is not de
, it's d
applied to e
.
Upvotes: 35
Reputation: 2079
An answer to your question can be found using :h exclusive
:
A character motion is either inclusive or exclusive. When inclusive, the
start and end position of the motion are included in the operation. When
exclusive, the last character towards the end of the buffer is not
included.
You can check, using :h word-motions
, which motions are inclusive (like e) and which are exclusive (like w). For using motions just to move cursor it doesn't matter but it does when using them in operator-pendig mode.
Note that this is in no way specific to Vim, those semantics were defined by original Vi.
Upvotes: 20
Reputation: 195129
this is because, the motion w
is exclusive motion
, but the e
is inclusive
one.
see:
:h w
:h e
and
:h exclusive
Upvotes: 9