Yanking multiple inner words in vim

vim

The inner and outer word text objects in vim behave differently when combined with a count. Given the text (cursor is within the word first)

first second third fourth

both y2iw will put the first word in the register, y3iw/y4iw the first two word etc. So I need at least y7iw to yank all four (inner) words. For outer words, the count given corresponds with the number of words yanked.

Why do two iw objects correspond to a single word in this case, and can I get iw to behave similar to aw?

Best Answer

Type 5 or so spaces and do viw: the 5 or so spaces are seleced because iw also considers \s+ as a separate "object".

In your example, you have 7 iw objects:

  • yiw yanks first in the unnamed register.
  • y2iw yanks first (with the trailing space) in the unnamed register.
  • y3iw yanks first second (with the space) in the unnamed register.
  • y4iw yanks first second (with the spaces) in the unnamed register.
  • And so on…

On the other hand, aw considers the word and any number of following space as an "object".

In your example, you have 4 aw objects:

  • yaw yanks first (with the trailing space) in the unnamed register.
  • y2aw yanks first second (with the trailing space) in the unnamed register.
  • y3aw yanks first second third (with the trailing space) in the unnamed register.
  • y4aw yanks first second third fourth in the unnamed register.

If you want iw to behave like aw just use aw.

Related Question