WALID BELRHALMIA
WALID BELRHALMIA

Reputation: 441

text object in Vim (moving between text object)

I have two words:

word1  word2

I do:

c2iw   

for changing two entire world but it doesn't work and it's the same for selecting:

v2iw

Upvotes: 1

Views: 594

Answers (4)

Sandeep Tuniki
Sandeep Tuniki

Reputation: 403

As others have pointed out, the iw motion considers the spaces as words too. So, if u don't want to be bothered about those spaces and target the words as you know them, use aw motion.

So, instead of doing this:

c3iw
v3iw

just do this:

c2aw
v2aw

Then Vim will not consider the space character as a word.

Upvotes: 0

michieldewilde
michieldewilde

Reputation: 46

First you need to jump to the next text object. You do this by using e.

e

The next step would be to change the two words that are next.

c2e

Change the next two text objects. For selecting them:

v2e

Upvotes: 1

romainl
romainl

Reputation: 196526

iw covers word1 and word2 but it also covers the whitespace between the two:

word1 word2
-----          iw
------         2iw
-----------    3iw

The correct command is thus:

<operator>3iw

or:

3<operator>iw

Upvotes: 1

Sato Katsura
Sato Katsura

Reputation: 3086

It works, but you need to count the iw commands, not the words. If the cursor is on a space, viw selects the spaces between words. In your case the first iw would refer to the first word, a second one to the space(s) between words, and a third one to the second word. Thus, you need c3iw and v3iw. Can't blame Vim for too much consistency. :)

Upvotes: 4

Related Questions