Change last word in line in Vim

I use Vim to edit C code, and I like to use c<movement> to quickly change parts of my file. In particular, I find things like c2W useful for "changing two words separated by spaces."

But there is one case that eluded me: when my cursor is at the last character of a word, the cb command, "change the word back" does not delete the last character of the word that I want to edit. This makes sense since the cursor was technically up to that last character, but that is not what I want.

Here, what happens, with the cursor marked | :

Start with:

 This is just a typ|ical series of words 

I would like to change “typical” to “different”. I press e :

 This is just a typica|l series of words 

And then cb :

 This is just a |l series of words 

Now there hangs l (lowercase L, to be clear) left! This is not what I wanted.

The more discerning of you have already noticed that in this example I could just use the combination bcw , which would put my cursor at the beginning of the word and then change it all over the word. True! But what if I wanted to change the “words” to “lines”?

Start with:

 This is just a typ|ical series of words 

I would like to change the “words” to “lines”. I press $ :

 This is just a typical series of word|s 

Same problem! Pressing cb will cause the s to freeze (although in this example it will be good, since the strings end with s anyway). Of course, I could press bcw , but this is another key and therefore unacceptable.

Do I just need to live with these incredible difficulties?

+4
source share
3 answers

You can use the i w motion ("inner word") to select the whole word that contains the current cursor. Thus, in combination with the c ("change") command, you and c i w .

This also applies to your first example: instead of first jumping to the end of a word with e and then using c b to reverse the word, you can also use c i w from the current position.

+10
source

Try one of these guys: c a w or c i w .

This way you can replace the word while your cursor is in the middle of the word.

+4
source

try the following:

ciw - change inner words.

+2
source

Source: https://habr.com/ru/post/1486668/


All Articles