简体   繁体   中英

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.

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:

If you only want to copy to the system clipboard in certain situations, you can use the "* register instead of setting clipboard globally, eg: "*yy .

Alternatively if you want to ensure that a command does NOT go to the system clipboard, you can use a different register or the black hole register "_ .

For more on registers:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM