In many cases, I have to replace a line by another in multiple places.

I can yank my desired line, go do the replacement location, do a pdd, move on, and of course I don't have the same line available for pasting.

Is there a better way of doing this?


3 Answers 3


Assuming you grabbed the first line with a y command, it should be in register 0. So you can do "0pdd every time (or, let @q = '"0pdd' and run @q/@@ for slightly less typing).

If the target lines are all in the same buffer as the original line, you can use a command like

:[from line]copy [to line] | [to line]delete

For example: :2copy 5 | 5delete. We can even wrap like this:

:command -nargs=1 -range Replace mark ' | <line1>,<line2>copy <args> | '' | <args>delete

where the range argument to Replace determines what line(s) to copy, defaulting to the current line, and the argument gives and address for the target (which is then deleted). The extra mark ' and '' commands resets the current line to its current position when starting the :delete, since an address (<args>, here) can be a search pattern and we want to make sure both uses target the same line.

Starting with


we have the following examples:

  1. gg:2Replace /a/ turns the second a line into b
  2. G:Replace 4 turns the c line into d.
  3. :1,3Replace 4 turns the line 4 into multiple lines. etc.

Note that arguments like $, whose actual line number changes after executing copy, may behave unexpectedly. (Perhaps deleting first would help?)


Instead of pdd, simply select the line you want to be replaced with V and then use P. That's it.

The reason to use the upper-case P instead of p is that it preserves registers, allowing you to do the same again.

Note that upper case P is a recent feature, around patch 8.2.4881.

  • 2
    I think you want to use VP instead of Vp Commented Sep 7, 2022 at 9:43
  • Right, thanks for correction. I haven't sleep this night.
    – user90726
    Commented Sep 7, 2022 at 9:53

Another answer covers how to replace a line with contents of a register. However, you'll find with doing VP, you'll lose the ability to repeat it via ..

I maintain a very small plugin which solves this visual put repeat problem.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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