打开Vim,并在缓冲区中仅插入以下文本行.
hello world
Run Code Online (Sandbox Code Playgroud)
换句话说,按i,键入hello world
并按Esc.
按0将光标定位在第一行的第一个字符处.
o
.按de.你会看到,从人物h
到o
已被删除.仅剩下以下文字.
world
Run Code Online (Sandbox Code Playgroud)打开Vim,并在缓冲区中仅插入以下文本行.
hello world
Run Code Online (Sandbox Code Playgroud)
换句话说,按i,键入hello world
并按Esc.
按0将光标定位在第一行的第一个字符处.
w
.按dw.你会看到,从人物h
到 have been deleted. Only the following text is left.
world
Run Code Online (Sandbox Code Playgroud)
However, I was expecting everything from h
到w
被删除,并只留了下面的文字.
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从移到h
至o
果然一切从h
到o
(包括h
和o
)被删除.
在实验2中,运动由于w从移到h
到w
而是从一切h
到w
(包括h
和w
不删除).为什么?
的行为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
.
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定义的.
这是因为,动议w
是exclusive motion
,但是e
是inclusive
一个.
看到:
:h w
:h e
Run Code Online (Sandbox Code Playgroud)
和
:h exclusive
Run Code Online (Sandbox Code Playgroud)