3

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?

| |
5

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.

| |
  • Thanks! The Vim documentation defines a word as "a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space". That's why I did not expect a single space character to be treated as a word. – janeden Jan 26 '14 at 10:35

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.