Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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), then go to the other place and do the yw (yank word), then go back to the replacement area and do p (paste) which is kinda clumsy especially if they are not on the same screen.

share|improve this question

3 Answers 3

up vote 5 down vote accepted

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.

share|improve this answer
1  
Option 3 (even simpler): With your word yanked, cursor over the first character of the word you want to replace and do vep. –  tlo Sep 9 at 22:10

yw to yank your word, then move the cursor to the word you wish to replace and use "_dw to delete it, sending the text to the null register (so it doesn't overwrite the contents of the " register, where yanked/cut text goes by default), and then simply paste with p.

You could use the following mapping to make things a little easier:

nnoremap <leader>d "_d

...so in normal mode, you could use \dw to delete a word, without affecting the " register.

share|improve this answer

I need this so often, I wrote a plugin to simplify and allow maximum speed: ReplaceWithRegister.

This plugin offers a two-in-one gr command that replaces text covered by a {motion} / text object, entire line(s) or the current selection with the contents of a register; the old text is deleted into the black-hole register, i.e. it's gone. It transparently handles many corner cases and allows for a quick repeat via the standard . command. Should you not like it, its page has links to alternatives.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.