为什么d {motion}命令与Vim中的{motion}命令不一致?

Lon*_*ner 30 vim

实验1

  1. 打开Vim,并在缓冲区中仅插入以下文本行.

    hello world
    
    Run Code Online (Sandbox Code Playgroud)

    换句话说,按i,键入hello world并按Esc.

  2. 0将光标定位在第一行的第一个字符处.

  3. e.光标移动到o.
  4. 0将光标再次定位在第一行的第一个字符处.
  5. de.你会看到,从人物ho已被删除.仅剩下以下文字.

     world
    
    Run Code Online (Sandbox Code Playgroud)

实验2

  1. 打开Vim,并在缓冲区中仅插入以下文本行.

    hello world
    
    Run Code Online (Sandbox Code Playgroud)

    换句话说,按i,键入hello world并按Esc.

  2. 0将光标定位在第一行的第一个字符处.

  3. w.光标移动到w.
  4. 0将光标再次定位在第一行的第一个字符处.
  5. dw.你会看到,从人物h have been deleted. Only the following text is left.

    world
    
    Run Code Online (Sandbox Code Playgroud)

    However, I was expecting everything from hw被删除,并只留了下面的文字.

    orld
    
    Run Code Online (Sandbox Code Playgroud)

首先让我引用:help d下面的内容.

                                                        *d*
["x]d{motion}           Delete text that {motion} moves over [into register
                        x].  See below for exceptions.
Run Code Online (Sandbox Code Playgroud)

在实验1中,运动由于e从移到ho果然一切从ho(包括ho)被删除.

在实验2中,运动由于w从移到hw而是从一切hw(包括hw不删除).为什么?

的行为dw,de以及db总结如下.

Command    Deletes character under the    Deletes character under the
           initial cursor position?       final cursor position?
-------    ---------------------------    ---------------------------
dw         Yes                            No
de         Yes                            Yes
db         No                             Yes
Run Code Online (Sandbox Code Playgroud)

为什么三个命令的行为不一致?

rom*_*inl 33

de削减光标下的角色,包括世界的最后一个角色,包括e一个包容性的动作.

dw削减光标下的所有内容,包括下一个单词,排除下一个单词,w是一个独占运动.

你的问题的答案不在:help d(de并且dw与之完全一致)但在:help e:help w(e并且w不必同样工作,因为正如文档所说,一个是包容性的而另一个是排他性的).

永远记住,Vim中的所有内容都是关于可组合性的:de不是de,它d适用于e.

  • @ Two-BitAlchemist:这不仅仅是解释它的一种方式.看看`:h operator-pending`. (6认同)
  • @ Two-BitAlchemist,我对Vim的内部结构并不了解,但是在经历了艰难的开始后,我突然"发现"Vim中的这么多东西实际上非常有意义.AFAIK,Vim的文档并没有真正解释"Vim作为一种语言"的概念:这是一个严重的监督IMO.学习口语可以通过两种方式完成:要么记住几十个标准句子,要么学习语法/语法并随时添加词汇.太多人不知道后者是学习Vim的唯一好方法. (3认同)
  • "...... Vim中的所有东西都是关于可组合性的:`de`不是'de`,它是'd`应用于'e`." 我喜欢这个解释.我没有看过vim的源代码:你知道这是否真的反映在"引擎盖下",或者这只是你选择解释它的方式? (2认同)

Krz*_*ski 19

您可以使用: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.
Run Code Online (Sandbox Code Playgroud)

您可以检查,使用:h word-motions哪些动作是包含的(如e)和哪些是独占的(如w).对于使用运动只是为了移动光标它无关紧要,但它在operator-pendig模式下使用时也是如此.

请注意,这绝不是特定于Vim的,这些语义是由原始Vi定义的.


Ken*_*ent 9

这是因为,动议wexclusive motion,但是einclusive一个.

看到:

:h w
:h e
Run Code Online (Sandbox Code Playgroud)

:h exclusive
Run Code Online (Sandbox Code Playgroud)