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

How do I use a count with text objects to operate on multiple text objects at once?

Answer

d2iw

Explanation

Text objects in Vim accept a count, letting you operate on a span of multiple adjacent text objects in one command. For iw (inner word), Vim alternates between selecting the word and then the surrounding whitespace, so d2iw deletes the current word plus the whitespace that follows — not the next full word.

How it works

The pattern is {operator}{count}{text-object}:

  • {operator}d (delete), c (change), y (yank), etc.
  • {count} — how many iw units to include
  • {text-object}iw, aw, i(, a", ap, etc.

With iw, each successive unit alternates: word → whitespace → word → whitespace. So the count determines how far the region extends.

Example

Given the line (cursor on one):

one two three four
  • d2iwtwo three four (word + following space = one )
  • d4iwthree four (word + space + word + space = one two )
  • d2awthree four (2 "around word" objects each include trailing space)
  • y2ap — yanks two paragraphs at once

Tips

  • Use d2aw or d4iw when you want to delete two complete words with their whitespace
  • Works with all text objects: 2i", 2a(, 2ap, 2it — each has its own expansion semantics
  • Count goes between the operator and text object: d3iw, not 3diw (though both work in practice for most text objects)

Next

How do I make Neovim restore the scroll position when navigating back through the jump list?