5

Consider the following text snippet:

value1,
    value2

Now, if the cursor is at (1,1) (the 'v' of value1), W takes me to (2,5) (the 'v' of value2) which is as expected. But if I do dW at the same position, what I expect is:

value2

But what I get is:

<empty_line>
    value2 

From :h exclusive:

Which motions are linewise, inclusive or exclusive is mentioned with the
command.  There are however, two general exceptions:
1. If the motion is exclusive and the end of the motion is in column 1, the
   end of the motion is moved to the end of the previous line and the motion
   becomes inclusive.  Example: "}" moves to the first line after a 
   paragraph,
   but "d}" will not include that line.
                                            exclusive-linewise
2. If the motion is exclusive, the end of the motion is in column 1 and the
   start of the motion was at or before the first non-blank in the line, the
   motion becomes linewise.  Example: If a paragraph begins with some blanks
   and you do "d}" while standing on the first non-blank, all the lines of
   the paragraph are deleted, including the blanks.  If you do a put now, the
   deleted lines will be inserted below the cursor position.

I am not sure if either of the exceptions applies to my case. What's going on?

:version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jan  9 2018 04:14:45)
Included patches: 1-662
Modified by <bugzilla@redhat.com>
Compiled by <bugzilla@redhat.com>
Huge version without GUI.  Features included (+) or not (-):
...
  • 1
    Noting here that dw also has the same issue. Surprisingly, however, dB and db work as expected. – stillanoob Apr 3 at 10:55
3

some contents of :help WORD

Another special case: When using the "w" motion in combination with an
operator and the last word moved over is at the end of a line, the end of
that word becomes the end of the operated text, not the first word in the
next line.

There is another special case if there exists trailing space after current word:

word0           
^       ^^^^^^some trailing space
word1

Assume cursor at "w" of "word0", dw will delete all trailing space after "word0". So to be precise, it should be "the end of line of current word becomes the end of operation text".

  • Ah! just another weirdness of vim. Nice catch. – stillanoob Apr 5 at 2:33
1

I don't think those exceptions apply because the end of the motion is not in column 1.

:h WORD says:

Another special case: When using the "w" motion in combination with an operator and the last word moved over is at the end of a line, the end of that word becomes the end of the operated text, not the first word in the next line.

One solution is to do Jwd0 or Jwd| instead.

  • 1
    W do include white space. dW will delete until but not include last character of W motion on common case. – dedowsdi Apr 4 at 9:15
  • It is because it does not include white space that it deletes until the last non-whitespace character. See :h WORD. – Tanath Apr 4 at 11:57
  • 1
    w forward to next word, words are seperated by white space, so w must include whitespace, it deletes until non-whitespace because w is exclusive. But :h WORD do give clear answer to this problem, it's a special case when "w" motion in combination with an operator and the last word moved over is at the end of a line. You should post that as the answer. – dedowsdi Apr 4 at 12:12
  • :h W specifically says it moves a WORD forward, exclusive. :h WORD says a WORD consists of non-blank characters separated by white space. When W moves over white-space it's because it's excluding it. When dW stops at white-space it's because it's excluding it. – Tanath Apr 4 at 21:32
  • 1
    @Tanath dW (or dw) does delete whitespace(s). Try it out for word1 word2 anywhere in word1. The blank between the two words will be removed. So, the statement, When dW stops at white-space it's because it's excluding it., is incorrect. – stillanoob Apr 5 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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