By default, the paste commands use the " (“unnamed”) register. Effectively, any command that writes to a register also writes to the unnamed register, so yanks, deletes, and changes all affect it. This is why your yank-delete-paste sequence pastes the deleted text instead of the yanked text.
The 0 register can help here. Any yank commands that do not specify a register put the yanked text in register 0 (in addition to "). It is not affected by delete or change operations, so you can use it to paste a yanked line multiple times even if you do intermediate deletes or changes.
yy: Registers0and"both now have the yanked line.- Move to a line to replace.
dd: Register"now has the deleted line, but register0still has the yanked line.
"0P: Paste the originally yanked line from register0.- Move to the next line to replace.
dd"0P(same as above)
(Due to the way cursor positioning works when replacing the last line of a buffer, you will want to use "0p instead of "0P.)
This is very close to Bruce Ediger’s answerBruce Ediger’s answer, except that you do not have to specify a register when initially yanking. Using one or more named registers can be very handy though if you need to (for example) replace some lines with AAA, but other lines with BBB (put AAA in register a, and BBB in register b (or leave one of them in register 0), then paste them accordingly).
You can also paste from 0 in line-wise visual mode (V) to save a keystroke: V"0p.
If you do not like having to type "0, you might find a mapping more convenient:
noremap <Leader>p "0p noremap <Leader>P "0P vnoremap <Leader>p "0p An alternate approach is to delete to the _ (“blackhole”) register. When you delete to it, the " register is not affected, so your yank-delete-paste sequence can still paste the yanked text from the unnamed register.
yy: Register0and"both now have the yanked line."_dd: No change to the registers.
P: Paste the originally yanked text from register".
Again, you might find a mapping more convenient:
noremap <Leader>d "_d