vimtricks.wiki Concise Vim tricks, one at a time.

How do I use a count with a text object to operate on multiple words, sentences, or paragraphs at once?

Answer

d3aw

Explanation

Most Vim users know you can put a count before an operator (3dw) or use a text object once (daw). What is less well-known is that you can put the count on the text object itself: d3aw means "delete 3 a-word objects", cleanly removing three words with their surrounding whitespace in one motion. This technique works with any operator and any text object.

How it works

  • d — the delete operator
  • 3 — count applied to the text object (not the operator)
  • aw — "a word" text object (word plus one side of surrounding whitespace)
  • 3aw selects three consecutive aw text objects, merging them into one contiguous region

The count extends the text object forward: 3aw from the start of the first word selects that word and the next two words (plus whitespace between them).

Example

the quick brown fox jumps

With cursor on the, running d3aw yields:

fox jumps

Three words (the, quick, brown) plus their trailing whitespace are deleted.

Tips

  • Works with all text objects: c3iw, y2is, =4ip, v5i{
  • d3aw vs 3daw: the former deletes a 3-word region; the latter deletes a word three separate times (same result here, but the undo granularity differs)
  • Pair with visual mode: v3aw selects three words, then you can use any visual operator
  • Works with custom text objects provided by plugins (e.g., d3if for three function bodies)

Next

How do I open the directory containing the current file in netrw from within Vim?