1

Consider the sentence

The sky is very blue today.

Let's investigate what happens when we use the vim command daw with the cursor placed at different positions.

  1. Cursor at any character of "blue"

The sky is very today

If we were to add a bunch of spaces between "blue" and "today", daw would still give this same result. It seems like daw deletes the word the cursor is in plus trailing whitespace.

  1. Cursor at any character of "today"

The sky is very blue.

In this case, it seems daw deleted the word and leading whitespace.

If we were to add a space between "today" and the period, then daw would behave like case 1).

Now, the help documentation for aw says

"a word", select [count] words (see word). Leading or trailing white space is included, but not counted.

I would have expected daw to delete the word and trailing and leading whitespace based on this documentation.

What are, after all, the different cases in which daw has distinctive behavior?

When I look at the documentation for word I see the following information

A word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, ). This can be changed with the 'iskeyword' option. An empty line is also considered to be a word. A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a WORD.

From the wording here, I am not sure if there is a distinction between word and WORD or not. They seem to be the same thing (in which case, why the two entries in the documentation?)

2 Answers 2

6

How aw (not daw) handles leading and trailing whitespace is unrelated to word or WORD so there is no reason to bring them up. The difference between word and WORD only matters when comparing aw and iw to aW and iW, which is off topic.

aw must be considered in contrast with iw: if iw covers blue, then what should aw cover? The answer is relatively obvious: "what iw covers, plus leading/trailing whitespace".

But how to handle that whitespace? If we systematically trim whitespace from both the front and the back, we leave the text in a poor state:

The sky is very blue today.
               ^^^^^^
The sky is verytoday.

In order to avoid situations like this, Vim uses a simple algorithm that can be summarized like this:

  • if the word is surrounded by whitespace, then cover the word + trailing whitespace,

    The sky is very blue today.
                    ^^^^^
    
  • if the word only has trailing whitespace, then cover the word + trailing whitespace,

    The sky is very blue today.
    ^^^^
    
  • if the word only has leading whitespace, then cover the word + leading whitespace,

    The sky is very blue today.
                        ^^^^^^
    
  • if the word is not surrounded by whitespace, then only cover the word.

1

About word vs. WORD:

To understand word you must group the character set into three families:

  1. Spaces (tab or space)
  2. Normal character (defined by iskeyword)
  3. The rest (typically punctuation)

A word is a contiguous sequence of the last two family of characters (Normal, The rest)

Here is what the doc explain it (:help word):

A word consists of a sequence of letters, digits and underscores, or a
sequence of other non-blank characters, separated with white space (spaces,
tabs, <EOL>).  This can be changed with the 'iskeyword' option.  An empty line
is also considered to be a word.

To understand WORD you must group the character set into two families:

  1. Spaces (tab or space)
  2. The rest

A WORD is a contiguous sequence the last family of characters (The rest)

Here is what the doc explain it (:help WORD):

A WORD consists of a sequence of non-blank characters, separated with white
space.  An empty line is also considered to be a WORD.

e.g alpha-blondy is three words but only one WORD.

About the behavior of daw you are right:

  • The spaces before the word are deleted if it is the last word of the line
  • The spaces before the word are deleted otherwise

For daW the logic is the same but with WORD instead of word

  • The spaces before the WORD are deleted if it is the last word of the line
  • The spaces before the WORD are deleted otherwise

Here is what the documentation says (:help 04.8)

The "d" of "daw" is the delete operator.  "aw" is a text object.  Hint: "aw"
stands for "A Word".  Thus "daw" is "Delete A Word".  To be precise, the white
space after the word is also deleted (or the white space before the word if at
the end of the line).

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.