Search code examples
vimcopy-paste

How to copy only yanked text to system clipboard in vim


I can copy to system clipboard using clipboard=unnamedplus. However this also copies text to system clipboard when using commands like dd or ciw.

For example, if I copy some text, and then use ciw, the text that I am replacing gets copied in the system clipboard and I have to copy the original text again to be able to paste it.

What I want is to copy to system clipboard only when I yank(y) a selection, or use commands such as yy, yiw or similar.


Solution

  • The solution is to put this in my .vimrc:

    " Use system clipboard by default
    set clipboard=unnamedplus
    
    " Remap 'c', 'C', 'd', 'D', 'x' and 'X' to save text in a custom register
    nnoremap c "cc
    vnoremap c "cc
    nnoremap C "cC
    vnoremap C "cC
    
    nnoremap d "dd
    vnoremap d "dd  
    nnoremap D "dD
    vnoremap D "dD
    
    nnoremap x "xx
    vnoremap x "xx
    nnoremap X "xX
    vnoremap X "xX
    

    I have found the solution here: