When I use "+y
to copy to the system clipboard, the selected content is copied to the +
register as well as the default(") register. How can I ensure that the content is copied only to the +
register without modifying the "
register?
EDIT:
I now understand that this is the intended behavior and any yanking will modify the default register. I still want to know whether there's a workaround for this.
I have come with a naive solution
for visual mode
vnoremap <Leader>y :<C-u>let @+=@*<CR>
and for single line in
nnoremap <Leader>yy :<C-u>let @+=getline('.') . "\n"<CR>
But we will be missing many yank features. I hope someone provides a better solution.
Best Answer
You need to define a custom operator;
:help :map-operator
has the details and an example. With that function, you can then also easily implement the visual mode (already shown in the example) andyy
mappings (usev:count . 'yy'
) easily.To avoid that the yank clobbers the default register, wrap the logic in the following that saves and restores that register: