How to delete a word in Vim
In Normal mode, with the cursor on the first letter of the word, press dw. From anywhere inside the word, daw deletes the whole word and the space after it.
Edit an email draft
Three stray words. The cursor is on the start of the repeated "the": dw it. On the next line the cursor lands inside the email address, which is full of punctuation: daW takes it in one go. Then daw takes "very" from the middle.
This editor needs a keyboard. Read on, and play it at your computer.
| Keys | Deletes |
|---|---|
| dw | from the cursor to the start of the next word, so the space after goes too |
| de | from the cursor to the end of the word; the space after stays |
| daw | the whole word and the space after it (or before it, at the end of a line), from anywhere in the word ("a word") |
| diw | just the word, from anywhere in it, leaving the spaces either side ("inner word") |
| db | back to the start of the word (the character under the cursor stays) |
| 3dw / d3w | three words |
| dW / daW | a WORD: everything up to the next space, punctuation included (ops-team@example.com) |
| ciw | the word, then Insert mode to type its replacement |
| Ctrl-w (Insert mode) | the word before the cursor, without leaving Insert mode |
dw or daw?
dw is an operator and a motion: delete from here to where w would jump. It only takes the whole word when the cursor is on its first letter; from the middle of a word it deletes just the end. daw uses a text object, "a word", which means the whole word wherever the cursor is in it, plus the space that separates it from the next. That makes daw the safer habit, and it also repeats well: . after daw deletes whichever word the cursor is on next.
Reach for diw when you want the gap to stay, usually because you will type something else there, and then ciw is shorter still.
Words and WORDs
For Vim, a word is a run of letters, digits and underscores, or a run of other non-blank characters, so ops-team@example.com is seven words and dw stops at each -, @ and .. A WORD (capital) is anything between spaces. Use dW or daW for addresses, paths, URLs and anything else with punctuation in it.