25

When I have to replace a word with another word I yank before, I usually use a workflow which seems unnatural to me:

Let's say I have this text:

foo bar buzz

And I want to yank foo and replace bar with what I yanked before.

After yanking foo, I see the following options:

Option 1

  1. Place the cursor on bar with w

  2. Put foo in front of bar with P

  3. Put the cursor on the b of the remaining bar and delete the word with de

Option 2

  1. Put the cursor on bar with w

  2. Use de to erase bar

  3. Put the content of the register 0 with "0P (since I erased bar, foo isn't accessible anymore with a simple p)

Both ways seems over-complicated to me and I'm sure there is a more efficient way to achieve this action but I can't find how. This idea would be to combine the delete and the put actions in only one, or at least avoid to keep the deleted word in the yank register so as I can put the desired word with a simple p.

Also I know the replace mode triggered with R but it doesn't allow to put what was yanked.

TL;DR: How to replace a word with the content of the yank register?

31

Assuming you've already yanked foo with ye or something similar, and that the cursor is somewhere on foo, you can use Wvep:

  • W to go to bar (this is obviously optional and will depend on the current cursor position);
  • v to start visual mode;
  • e to go to the end of the word (you can also use other motions here, like iw);
  • p to paste foo; this will replace the current visual selection (which is put in the unnamed register, "").
6

I have found another way to do it, it is a little bit similar to the option 2 but it has the advantage of not messing the state of the registers.

The idea is to use the "black hole register": This register is accessible via "_ and has the particularity that you can write into it but not read from it, so like a black hole this register makes totally disappear anything which goes into it. Here we can use it so foo stays in the yank register even after bar deletion.

With this solution the worflow after yanking foo would be:

  • Going to bar with W (or any needed word of course)
  • Using the "black hole register" to delete bar with "_de
  • Putting foo from the yank register with P. The yank register still contains foo since bar went into the black hole.

I'm not sure that's is a better solution than @Carpersmoker's one (In term of vimgolf @Carpetsmoker wins) but I didn't know about the black hole register and I thought some might be interested to know it.

3
  1. Go to start of "foo". Press yiw. That yanks the inner word.

  2. Go to "bar" pressing the letter w. Then select it and yank back: viw"0p

I use ciw and yiw a lot.

  • 4
    The use of text objects like iw is interesting but your solution is actually the same as @CarpetSmoker's one: visually selecting the second word and put the first one. I also think that your solution would work with a simple p instead of using "0 register. – statox May 25 '15 at 16:05
1
  • When the cursor is anywhere on the word,

    • yiw - yank the word and store it in 0 register.
  • Go to the target word to be replaced

    • ciw will delete the word and put you in insert mode.
    • ctrl-r. A " will pop up asking the register to be invoked.
    • 0 will paste the yanked word.

Yanked words are (normally!) stored in 0 register. See :registers to view the stored buffers. This may seem a lengthy procedure, IMHO this opens the door to the much more sophisticated register tricks.

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.