0

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.

Andrija Čehko
  • 443
  • 8
  • 14

2 Answers2

2

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:

Daniel Waechter
  • 2,574
  • 2
  • 21
  • 21
  • This doesn't really help. I know I can manually use the registers. What I am looking for is to somehow override the `y` command to copy to the clipboard without having to write `"*` every time. – Andrija Čehko May 19 '17 at 08:51
2

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:

Community
  • 1
  • 1
Andrija Čehko
  • 443
  • 8
  • 14