How do I replace-paste yanked text in vim without yanking the deleted lines?
I have these mappings in my .vimrc:
" delete without yanking
nnoremap <leader>d "_d
vnoremap <leader>d "_d
" replace currently selected text with default register
" without yanking it
vnoremap <leader>p "_dP
"_
is the "blackhole register", according to :help "_
:
"When writing to this register, nothing happens. This can be used to delete text without affecting the normal registers. When reading from this register, nothing is returned. {not in Vi}"
In addition to the standard buffer, you can yank text into named buffers, and then put from those named buffers. There are up to 26 named buffers you can use (one for each letter). Use double quotes and a letter to access a named buffer. Examples:
"dyy
- Yank current line into buffer d.
"a7yy
- Yank next seven lines into buffer a.
"dP
- Put the contents of buffer d before cursor.
"ap
- Put the contents of buffer a after cursor
Another cool thing, if you use a capital letter instead of lower case, i.e "Dyy
the current line will be Appended to the buffer d instead of replacing it. More details in the O`Reilly book: http://docstore.mik.ua/orelly/unix/vi/ch04_03.htm
When using put
in visual mode, the text you're replacing, wrong1
, is overwritten by the contents of the 'unamed' register.
This actually works by 'putting' the register after the selection and then deleting the selection. The problem is that this deletion is now stored in the unnamed
register and will be used for the next put
action.
The solution, according to :h v_p
, is to yank into a named register, such as "0y
, then paste using "0p
as many time as you need. It may be helpful to map <leader>y
and <leader>p
to use a named register, if this is something you do frequently.
:map <leader>y "0y
:map <leader>p "0p
for more help see:
:help v_p
:help map