"around word" doesn't behave consistently due to white space, why?

24 views
Skip to first unread message

Britton Kerin

unread,
Jan 19, 2024, 4:35:05 AM1/19/24
to vim...@vim.org
The sequence 'vaw' (visual around word is how I think of it) does
weird things depending on white space:

* 'vaw' while on 'a' in 'foo bar' selects ' bar' (space followed by bar)

* 'vaw' while on 'a' in 'foo bar ' (with trailing space) selects
'bar ' (bar followed by space)

Why?

I could understand (though not exact agree with the policy) if spaces
are being viewed as part of particular words in this context, but I
don't see why adding a space after bar causes the one before it to not
be selected.

Britton

Gary Johnson

unread,
Jan 19, 2024, 11:50:52 AM1/19/24
to vim...@googlegroups.com
I can't find where this is documented, but Vim defines "a word", for
most purposes, as a sequence of word characters followed by white
space. That definition can't hold at the end of the line, so the
developers were left with a choice of including the preceding white
space or not including any white space. The choice to include the
preceding white space is pragmatic.

If you want to cut a word from one place and paste it somewhere
else, it is convenient for that cut to include white space so that
you don't have to remove it separately from the cut location nor add
it separately to the paste location. There is no following white
space at the end of a line, so the preceding white space is included
instead. Also, if you delete a word at the end of a line,
presumably you don't want to leave white space at the end of that
line.

That logic is also applied in other situations where a sequence of
word characters is followed by a non-word character such as
a closing parenthesis.

Regards,
Gary

Tony Mechelynck

unread,
Jan 20, 2024, 5:04:03 AM1/20/24
to vim...@googlegroups.com
It is documented under ":help aw" (aw, i.e. "a word" being the
"motion" applied after v sets characterwise visual). «Leading or
trailing white space is included, but not counted.» IOW, white space
is included on one side (but not both). If Vim doesn't find white
space where it looks for it first (after the word IIUC) then it looks
on the other side.

Best regards,
Tony.
Reply all
Reply to author
Forward
0 new messages