78

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.

11 Answers 11

108

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.

  • 14
    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 '14 at 22:10
  • I like Option 3 when there is only 1 word to change. If there is more than one same word to change, Option 1 allows me to use the . operator to repeat it.. – typelogic Sep 20 '18 at 17:36
  • 3
    ve"0p would work to let you repeat your change – JKillian Feb 6 at 19:03
  • If you're going to map it to something, it's probably better to use: map <C-j> ciw<C-r>0<ESC> so that it doesn't matter from where in the word you press ^j. – Paul Parker Mar 3 at 0:47
  • Anyone wanting to know why CTRL-R does it's magic: :help insert then search CTRL-R /CTRL-R. For how ve"0p works, look at :help registers – Paul Parker Mar 3 at 0:50
12

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.

2

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.

2

To repeat the paste use the change command.

cw ^R0ESC (^R=Control+R; 0 for register 0)

will replace the word with the contents from reg. 0 and let's you repeat it with .

This is great for cgn (VIM 7.4) to replace search patterns:

  • First search with / or *
  • cgn ^R0ESC
  • . to repeat on next match or n to skip
1

Since it's not included yet, you usually replace strings with :%s/.../.../ and you can do this with the word under your cursor as well.

  1. You enter the first part of the command, what you are searching for: :s/find/
  2. Then you click <ctrl-r> and choose with the cursor the word you want to replace it with.
  3. Then you click <ctrl-w> (small "word") or <ctrl-a> (big "word) to paste the word under the cursor into the command line
  4. Then you finish the command.

See the link about how to use cursor stuff in the command line.

1

I usually use this: yw to yank foo, then delete bar with dw, and as final step, put foo from register 0 which holds yanked text using "0p. Repeat putting with period (.) as needed.

Also, instead of yw and dw, you can use yiw and diw (notice the extra i - it means inside). With that you can yank or delete word from any location inside it, not just on the beginning of word.

0

I use this plugin for expanding selection region: https://github.com/terryma/vim-expand-region

So my combination for changing a word will be: vvpin any place of a word.

I also have a hack for saving unnamed register contents after paste so I can repeat this.

vnoremap p p:let @"=@0 <CR>
vnoremap P P:let @"=@0 <CR>

(my clipboard is autoselect)

0

I'm using below key mapping to replace the current word with copied word.
" replace with Register 0
map <leader>rr ciw<C-r>0<Esc>

yiw copy a word. Navigate to a word. ,rr replace the word.
Leader key is map to ,.

  • ,rw would make more sense, and on QWERTY is probably a smidgen easier and faster to belt out. On DVORAK, I chose ,rc, which although it doesn't make much sense at all, it is easier and faster than all of them. On DVORAK, ,rw is a horror show ;) – Paul Parker Mar 3 at 1:00
0

First, navigate to the start of the word you want to yank. If you're in the middle of the word, just do b to go back to the start of it. Then do ye to yank the whole word, without any trailing spaces (yank to end). Finally, go to the word you want to replace - perhaps by doing w a few times - and do a vep to enter visual selection mode, select to the end of the word, and paste the contents last yanked.

(If anyone wants to know why I didn't simply comment on the accepted answer, for the small modification of using ye rather than yw, it's because my reputation on this stack is still too low.)

0
  1. use yw to yank the world, go to the world you want to change.
  2. use v to select the world, ve or vw depending on your situation.
  3. enter p to paster the world you copied to replace the selected block
0

To do this the VIM way, you intentionally use the yank, delete and other registers.

Register "0 is the yank register. Anything you yank will be put here, but deletes never touch register "0.

So, in your example, you had just yanked a word. To replace a word with what you just yanked, you take advantage of deletions never touching the yank register. So move to the target word, delete it with dw, then paste from your yank-register with "0p, or better yet, cw then ^R0 (which is repeatable).

A close opposite to the yank register is the small deletions register "-. Any small delete or change removal is put here, but yanks never touch "-. A deletion counts as small if it is less than a full line.

Registers "1-"9 are the delete history registers. With "1 containing the latest large deletion or change removal, and "9 containing the oldest large deletion or change removal. Only deletes that aren't small, i.e. deletes of one line or more, get pushed onto "1-"9.

For any operation that changes a register, a copy is also always placed in the default, a.k.a. unnamed register "". This is the register used when you don't explicitly name a register.

Register "_ is the black hole register, and it is always empty. If you delete to it, nothing in any register is changed at all, not even the default "" register, or the black hole register itself. The removed text is fully gone, apart from your undo history. Yanking to, or pasting from the black hole register does essentially nothing.

The black hole register "_ lets you do things like first one small deletion, then a number of other deletions into "_ without changing your small deletions register "-, then paste your first small deletion.

Other registers are the last inserted register "., the filename registers "% and "#, the command register ":, search register "/ and expression register "=.

You can get a list of all these registers and their contents by the command :register. That command is very useful to experiment with and learn what ends up where.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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