An editing pattern I frequently run into is the following: I want to yank a piece of text inside quotes or brackets, and replace that piece of text inside another pair of quotes/brackets.

For example, if I want to copy the the class name foo bar and paste that somewhere else:

<span class="foo bar">

<div class="this-should-be-replaced-with-foo-and-bar">

What I know is this: If I move inside the span's class, I use yi" to yank the text inside the quotes.

Now I have this in my register, so I should be able to replace the class content of the div with it. But I can't find a handy way of doing this.

If I use ciw this ends up overwriting my register with content to change, so I loose my yanked text.

The best thing I can come up with is moving to the first quote of the div's class, hitting p, then deleting to the next quote with dt".

Usually I screw up my position though, and I paste it somewhere inside the quotes merging the two strings, and it is messing things up.

I'm hoping there's some way to do a "replace the inner text with my yanked content please" combination to make this easier.


You can use vi"p when inside "". This can replace text in yank register, so it matters when you want to use original yanked text more than once.

EDIT: Additional info from Octaviour comment regarding making it reusable more than once:

In order to do that I would yank to a named register, which makes the two commands: "ayi" and and vi""ap if you use register a.

  • 2
    I'll elaborate on this solution. You first yank the text you want to copy. Then you go to the place you want to paste, visually select using vi". A paste command will now simultaneously put the contents of the unnamed register at the cursor and delete the selected text. Therefore vi"p accomplishes exactly what you want, but it does replace the contents of the unnamed register, disallowing you from pasting the same text twice. In order to do that I would yank to a named register, which makes the two commands: "ayi" and and vi""ap if you use register a. – Octaviour Nov 4 '16 at 10:59
  • Thanks for elaborating. It helps to spell things out with vim motions, it makes it easier to remember afterwards. It's still not as easy as I hoped it would be, but it makes sense in a vim way. Using the dot command to have it repeat and quickly replace a second time doesn't seem to work though. I hoped that would be a shortcut. :) – jurgenb Nov 7 '16 at 9:19

You can use the numbered register. First yank "foo-bar" with yi", which will fill the "0 register, see :h "0. Then do ci"<c-r>0 to replace with the content of the register 0.

See :h i_CTRL-R for a description of the last part.

  • 2
    Why not simply yanking it in the unnamed register (yi") and pasting it from the register 0 where it is stored after you deleted the text to change (ci"<c-r>0)? In term of vimgolf you win 2 keystrokes and you don't mess with your registers. – statox Nov 4 '16 at 10:29
  • as soon as I hit 'a' I end up in edit mode though, this doesn't copy to a register – jurgenb Nov 4 '16 at 10:36
  • @n3wjack That's probably because you didn't pressed CTRL+r before a, that's what <c-r> means. – statox Nov 4 '16 at 10:44
  • 1
    @KarlYngveLervåg Actually ciw does put the deleted text in the unnamed register but the registers are shifted, that's why you have to use <c-r>0 instead of <c-r>". See :h quote_number. – statox Nov 4 '16 at 10:48
  • 1
    I would think, more specifically, that the point here is not about the unnamed register, but instead about the numbered register. The "0 register is only filled by the yank command and will not be disturbed by e.g. c. – Karl Yngve Lervåg Nov 4 '16 at 10:49

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.

  • Great! This should be vanilla vim. – Philipp Moers Nov 6 '16 at 10:32

Here is another solution. It's not the best in term of Vimgolf, but it uses the black hole register:

Once you're on the foo bar string you can yank it normally using yi".

Then inside the brackets to change: "_di"P

Which is:

"_       We will use the black hole register
di"      Delete the inner brackets and put the deleted text in the black hole register
P        Put the previously yanked text

The advantage of the black hole register is that it will not mess your others registers. See :h quote_ for more details.

(Note that this answer is only here to talk about the black out register. I personally use visual selection as suggested by @grodzik.)

  • I think you need an upper case P for that one. When you di" your cursor will be left on the last quotes and p will paste after, where as P will paste before the current position. – jecxjo Nov 4 '16 at 14:38
  • @jecxjo That's right, I edited my answer, thanks! – statox Nov 4 '16 at 14:40

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.