15

This has always bugged me about the w and W motion commands—they seem to work differently when used with the change command than they do when used alone, or with the delete command.


For example, with the cursor on the "q" of this text:

The quick brown fox jumps over the lazy dog.

typing dw results in

The brown fox jumps over the lazy dog.

and typing de results in

The  brown fox jumps over the lazy dog.

(Notice the two spaces between "The" and "brown".)


The yank command also treats w and e differently, as can be shown by comparing yeP:

The quickquick brown fox jumps over the lazy dog.

and ywP:

The quick quick brown fox jumps over the lazy dog.

However, typing either ceslow<Esc> or cwslow<Esc> results in the same thing:

The slow brown fox jumps over the lazy dog.

Is this documented somewhere? What is the logic behind it and how can I predict the results of a command that uses w?

18

It's a special case. Check the documentation for WORD, under "Special case":

Special case: "cw" and "cW" are treated like "ce" and "cE" if the cursor is on a non-blank. This is because "cw" is interpreted as change-word, and a word does not include the following white space. {Vi: "cw" when on a blank followed by other blanks changes only the first blank; this is probably a bug, because "dw" deletes all the blanks}

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.

The original Vi implementation of "e" is buggy. For example, the "e" command will stop on the first character of a line if the previous line was empty. But when you use "2e" this does not happen. In Vim "ee" and "2e" are the same, which is more logical. However, this causes a small incompatibility between Vi and Vim.

6

The difference between the behavior of the motion in cw vs. dw can be explained simply: normally if you want to change a word you're going to leave the whitespace following it, while deleting a word suggests you want the whitespace to also be removed.

If you do want the whitespace to go away for some reason (I do it regularly enough but can't think of an example) then use the aw motion: caw.

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.