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?
Motions (as in movements) are how you move around in Vim. They are commands that when typed move the cursor around with high speed and precision. There are many of them, and each one is best suited for different types and lengths of movement.
Normal mode is where one should spend most of their time while using Vim. Remember, this is what makes Vim different. In normal mode, there are multiple ways to move around an open file. In addition to using the cursor keys to move around, you can use h (left), j (down), k (up), and l (right) to move as well.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With