31

Experiment 1

  1. 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.

  2. Press 0 to position the cursor at the first character of first line.

  3. Press e. The cursor moves to o.
  4. Press 0 to position the cursor at the first character of first line again.
  5. Press de. You'll see that the characters from h to o have been deleted. Only the following text is left.

     world
    

Experiment 2

  1. 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.

  2. Press 0 to position the cursor at the first character of first line.

  3. Press w. The cursor moves to w.
  4. Press 0 to position the cursor at the first character of first line again.
  5. 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
    

Question

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?

2
  • 1
    Probably this behaviour serves better the common usecases.
    – erenon
    Apr 13, 2014 at 10:02
  • 7
    Great presentation on this question. Very readable and easy to follow along. Apr 13, 2014 at 10:34

4 Answers 4

35

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.

4
  • 2
    "...everything in Vim is about composability: de is not de, it's d applied to e." I love this explanation. I haven't ever looked at vim's source code: do you know if this is actually reflected "under the hood", or is this just the way you're choosing to explain it? Apr 13, 2014 at 10:37
  • 7
    @Two-BitAlchemist: It's more than just a way to explain it. Take a look at :h operator-pending. Apr 13, 2014 at 10:41
  • 3
    @Two-BitAlchemist, I have no deep knowledge of Vim's internals but, after a rocky beginning I suddenly "discovered" that so much things in Vim actually made quite a lot of sense. AFAIK, Vim's documentation doesn't really explain the "Vim as a language" concept: that's a serious oversight IMO. Learning a spoken language can be done in two ways: either you memorize dozens upon dozens of standard sentences or you learn the grammar/syntax and add vocabulary as you go. Too many people don't know that the latter is the only good way to learn Vim.
    – romainl
    Apr 13, 2014 at 11:32
  • Thank you both. I've been experimenting with functional programming (curried functions, partial application) so the composition idea really resonated with me. :h operator-pending was an interesting read. I have experienced that mode many times and did not know it had a name! Apr 13, 2014 at 11:55
20

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.

9

this is because, the motion w is exclusive motion, but the e is inclusive one.

see:

:h w
:h e

and

:h exclusive
1

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
2
  • Why dve and vde behave completely different? Isn't the motion the same?
    – Anton
    Sep 8, 2021 at 22:39
  • @Anton, no, they are completely different. vde will select a single character at the cursor and delete it, then e moves to the end of the current word.
    – jdhao
    Sep 9, 2021 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.