4

My confusion started with realizing, that yW yanks a different region, then what vW selects, but I found this that explains about selection. But then I realized, that while yW will yank until the end of the current word I'm on, the motion W will actually go to the beginning of the next word. Why is this?

I'm also a bit confused about yanking with e, E vs. w and W.

Given a line like this, with the cursor at the start of the line:

title: hello

ye is the same as yw and yE the same as yW, while the motions themselves are different. Where is this explained? What is the rationale here? What's the complete picture?

I felt like I'm starting to be able to speak vim pretty well, but this really confuses me right now.

2
  • Read :help exclusive. Pay attention to whitespace you yank or not-yank. Try an example with only whitespace (and no colon) between words.
    – Friedrich
    Feb 2 at 22:54
  • I did read it, but it hasn't made things clearer, if the difference between E and W is their inclusivness, why do they yank the same (n.b. I tried without the colon and they still yank the same). It also does not explain why yW yanks less then what the motion W would cover.
    – fbence
    Feb 3 at 7:47

1 Answer 1

8

It might be useful to quote :help exclusive, here:

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.
Linewise motions always include the start and end position.

When used alone, in normal mode, W moves the cursor to the first character of the next :help WORD. It is neither "inclusive" nor "exclusive" because the notion of "exclusive" and "inclusive" only applies when the motion is used after an operator.

When used in visual mode, as in vW, W also moves the cursor to the first character of the next WORD, because visual mode forces all motions to be "inclusive". Using visual mode is actually how you can coerce a motion: :help forced-motions.

When used after an operator, as in yW, W is "exclusive" (as mentioned in :help W), which means that the last character is not included:

   start              end
   v                  v
qhfdksgqfdksqgdfskqgd qsdkytsqdfisyqtdr
   --------------------                    " inclusive motion
   -------------------                     " exclusive motion

Note that, because some motions are "exclusive" (like :help W) and others are "inclusive" (like :help e), doing :help <motion> is always a good idea.

As for yE versus yW, they are expected to yank different things because E is "inclusive" and W is "exclusive". In the example above, yW would yank:

dksgqfdksqgdfskqgd < with the space

while yE would yank:

dksgqfdksqgdfskqgd< without the space

Going with your example:

title: hello
  • yw and ye have identical outcomes because the next "word" happens to be :, see :help 'iskeyword', without whitespace.

    In reality, e covers title "inclusively" (therefore title), while w covers title: "exclusively" (therefore title), which boils down to the two motions both covering the exact same text.

    If title and : were separated by whitespace, the outcomes would be different, with ye yanking title and yw yanking title (with the space).

  • yW and yE have different outcomes because you are dealing with WORDs, separated by whitespace.

    W covers title: h "exclusively", therefore title: (with the space), while E covers title: "inclusively".

    This is easy to check by pressing P after each yank. After yWP, you get:

    title: title: hello
    

    and after yEP, you get:

    title:title: hello
    
1
  • 2
    Thanks! I also see what confused me: :h exclusive definitely implies it, but it was nkt explicit enough for me to realize that it only applies after an operator like yank. Btw I really appreciate the amount of detail you put into your answers!)
    – fbence
    Feb 3 at 11:54

Your Answer

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

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