Currently I have the following mapping in my vimrc

nnoremap d "_d
nnoremap D "_D
nnoremap C "_C
nnoremap c "_c

nnoremap <leader>d "*d
nnoremap <leader>D "*D"

The result of the above mapping is that everything that is yanked after deleting or changing goes to the black hole register.

Now the last problem I am having is after I paste onto a visual line, the replaced text gets yanked on to the clipboard (default) register. So I tried to use this function which attempts to solve this problem

function! RestoreRegister()
  let @" = s:restore_reg
  return ''
function! s:Repl()
  let s:restore_reg = @"
  return "p@=RestoreRegister()\<cr>"
vmap <silent> <expr> p <sid>Repl()

This does not seem to work and I am not sure why. Any help on this issue would be appreciated. Could this have to do with my custom mappings that I defined above?

1 Answer 1


I'm not sure what's wrong with your function (I didn't look super hard) but there is much simpler solution. Rather than pasting and then restoring the previous register, delete the visual selection to the black hole register before pasting. For example:

vnoremap p "_dp
vnoremap P "_dP

You could also provide similar mappings for when you want to capture this to the system clipboard:

vnoremap <leader>p "*dp
vnoremap <leader>P "*dP
  • 1
    – romainl
    Aug 22, 2016 at 6:27

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.