Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

vim - Rule for d{motion} for different motions

Given:

for (int i = 0; i < 10; i++){
   _   <---Cursor position

3w leads to

for (int i = 0; i < 10; i++){
         _   <---Cursor position

and d3w leads to

fori = 0; i < 10; i++){
   _   <---Cursor position

i.e., even though motion 3w takes cursor upto i, i itself is not deleted.

On the other hand, given:

for (int i = 0; i < 10; i++){
   _   <---Cursor position

% leads to

for (int i = 0; i < 10; i++){
                           _   <---Cursor position

and d% leads to

for{
   _   <---Cursor position

i.e., motion % takes cursor upto ) and ) itself is deleted.

So, why is there two different effects of d{motion}? Is there any single general rule of which both of these are consistent special cases?

question from:https://stackoverflow.com/questions/65643113/rule-for-dmotion-for-different-motions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, there's a logic to that. In Vim, some motions such as w are "exclusive", while other motions such as % are "inclusive". This will determine whether the action will affect the last character of the motion or not.

You can actually override the "exclusive" or "inclusive" status of a motion by using the v operator (note that v is being used as an operator here, not starting Visual mode as it does when used as a Normal mode command!) So dv3w (or d3vw) will delete up to the beginning of third word "inclusive" of the character it lands on, while dv% will delete up to the next matching bracket "exclusive".

In a way, Visual mode is somewhat similar, since a Visual selection is "inclusive" by default, so v3wd would behave similarly to dv3w. (Though this can be overridden by the 'selection' option.)

See:

  • :help w
  • :help %
  • :help exclusive (same as :help inclusive)
  • :help o_v
  • :help 'selection'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...