How to do a ‘change word’ in Vim using the current paste buffer

bufferreplacevivim

I have some text in my paste buffer, e.g. I did a yw (yank word) and now I have 'foo' in my buffer.

I now go to the word 'bar', and I want to replace it with my paste buffer.

To replace the text manually I could do cw and then type the new word.

How can I do a 'change word', but use the contents of my paste buffer instead of manually typing out the replacement word?

The best option I have right now is to go to the beginning of the word I want to replace and do dw (delete word), go to the other place, and do the yw (yank word). Then go back to the replacement area and do p (paste) which is kind of clumsy, especially if they are not on the same screen.

Best Answer

Option 1

You could use registers to do it and make a keybinding for the process.

Yank the word you want to replace with yw.

The yanked word is in the 0 register which you can see by issuing :registers.

Go to the word you want to replace and do cw. Do Ctrl+r followed by 0 to paste the 0 register.

The map for that would look something like this (assuming Ctrl+j as our key combo):

:map <C-j> cw<C-r>0<ESC>

Option 2 (simpler)

With your word yanked, cursor over the word you want to replace and do viwp. Which is visual select inner word and paste.

Courtesy of @tlo in the comments: you could also just do vep. One char shorter. Downside have to position cursor at start of word and (as with mine) changes the buffer.

Comment (from Michael):

This is good. Extra note: the second method is indeed easier but, as is, only works for ONE substitution because after each substitution the buffer then gets changed to the field that was replaced (old text). The first method is a little harder to use BUT has the advantage that the buffer 0 stays 'as is' so you can use that method to do more than 1 replacement of the same text.

Related Question