1

Suppose I've got the following sentence:

This |is a sentence for testing purposes.

I want this sentence to be

This purposes.

In fact I want to delete 5 words. Of course, I could do it just as d5w but this is so inconvenient and painful. I have to count the words first and then execute the appropriate combination.

I tried it the other way. I pushed d and then I tried to push w 5 times. But it started deleting lines (and that's sensible, because dd deletes a line).

Is there a way to perform word deletion like we did in general editors (we keep pressing Ctrl and then press Del 5 times) but in Vim. I suppose this quite common operation and there must be a "fast" solution for it.

  • 4
    dtp (delete till the next p) would work in this case. If you're not sure, vtp d so you see what would you delete. More general (, d/ p Enter to delete till the searched item, or again, to see what you're doing, v/ p Enter d ... – VanLaser Sep 26 '16 at 9:39
6

Meet your friend the dot command!

:h .

Press daw (or de) to delete a word and repeat the command with .

From the doc:

.          Repeat last change, with count replaced with [count].
           Also repeat a yank command, when the 'y' flag is
           included in 'cpoptions'.  Does not repeat a
           command-line command.

Note that the dot command works amazingly with the undo workflow of Vim. You can repeat the last change as many time as you want with . and if you realized you used it too many time simply use u to undo the change. Want to finally redo the change? Use Ctrl+r


Also as VanLaser noted in his comment there is other ways to delete several words. In addition to his dtp idea note that you can also do that:

  • Press d
  • Begin a search /
  • Type the pattern matching the end of the deletion (here purpose for example)
  • Press Enter
  • I already recommended it a lot of time on this site, but I think every one who want learn how to use Vim should read Practical Vim that's an amazing reference to learn the basic commands like . – statox Sep 26 '16 at 9:33
  • Alternatively: 5dw, or d5w (same thing in this particular case). The catches: you need to start at the beginning of the first word, and your count of 5 needs to be correct. – lcd047 Sep 26 '16 at 13:20
  • It's not worth posting as an answer, but it might supplement yours. You could also start visual mode and hit w until it looks like what you want to delete. Or, probably more practical would be hitting e a bunch of times. – DJMcMayhem Sep 26 '16 at 16:24

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.