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
: Registers 0
and "
both now have the yanked line.
- Move to a line to replace.
dd
: Register "
now has the deleted line, but register 0
still has the yanked line.
"0P
: Paste the originally yanked line from register 0
.
- 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 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
: Register 0
and "
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
You can :set virtualedit+=onemore
, which allows you to go just past the last character. Unfortunately for you, $
still goes to the last character; you have to cursor over to get past it. On the other hand, you can use g$
, which normally goes to the last character of the screen line on a line that wraps; in onemore
mode it also goes just past the last character of a non-wrapped line (or the last screen line of a wrapped line).
:h 'virtualedit'
says this option can mess up scripts, but I don't know how big a risk that is in reality.
Best Answer
Two suggestions to paste the contents on a line of its own:
You can use the
:put!
command, since it always works linewise. The version with the!
inserts the contents of the register before (rather than after) the current line. (You can abbreviate it to:pu!
.)You can use O, Ctrl+R, ", Esc to insert a line above with the contents of the latest yank. See help on i_CTRL-R for the Ctrl+R part. And
"
is the "unnamed" register, which is where yanks and deletes go by default. This is not necessarily "easier" than O, Esc, p, but it has the advantage that it's a single command, so it's repeatable with . and the whole action can be undone at once.If this is a frequent enough operation for you, consider creating a mapping for it, that would be surely the easiest one to type. :-)