What is difference between w and W in escape mode of vim?

According to this cheat sheet it would seem to come down to punctuation.

w   jump by start of words (punctuation considered words)
W   jump by words (spaces separate words)
e   jump to end of words (punctuation considered words)
E   jump to end of words (no punctuation)

Example

demo using w

          ss of w

demo using W

          ss of W

Notice in the first demo how when w is continuously hit that the cursor lands on the punctuation, while in the 2nd demo the punctuation is skipped. This is the difference between the lowercase commands and their upper case equivalents.


It's actually fairly simple, w just skips to the next word, but any sort of punctuation or whitespace breaks up words. W is like the industrial-strength next-word command, words are only delimited by whitespace, be it tab, space or newline. w stops at the delimiting punctuation also, in case you want to deal with that.

I find the difference useful most often with hostnames:

birdman.cartoons.fictional.com # Fictional host

With w, you stop on each part of the hostname and the dots. W just skips the entire hostname and goes onto the #. These both also apply to d and c for deleting and changing words.

If positioned at the beginning of the above line, if you wanted to replace birdman.cartoons with something else, you would use 3cw to replace both of the first two parts and the dot in the middle. You couldn't do it with W without retyping the whole hostname.


It is a difference that is best understood by considering how punctuation can impact on Vim's concept of what delimits a word.

In the case of this example text:

This line is space separated with no commas
however, this line is—note—comma, separated,see

w will happily skip to the first letter of each word in the first line, as there is only space to negotiate. It will fail badly on line 2 with the punctuation, alighting on the first letters of each word and the commas and en dashes. To move from word to word on line 2, you would need to use Shiftw1.

1. Even then, Vim sees "is–note–comma" as a single word.

Tags:

Vim