Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why d{motion} command is not consistent with {motion} command in Vim?

Tags:

vim

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?

like image 667
Lone Learner Avatar asked Apr 13 '14 09:04

Lone Learner


People also ask

What is motion in Vim?

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.

What are the motion commands in normal mode?

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.


2 Answers

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.

like image 164
romainl Avatar answered Oct 17 '22 04:10

romainl


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.

like image 20
Krzysztof Adamski Avatar answered Oct 17 '22 06:10

Krzysztof Adamski