5

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.

1 Answer 1

1

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) and yy mappings (use v:count . 'yy') easily.

To avoid that the yank clobbers the default register, wrap the logic in the following that saves and restores that register:

let l:save_clipboard = &clipboard set clipboard= " Avoid clobbering the selection and clipboard registers. let l:save_reg = getreg('"') let l:save_regmode = getregtype('"') " some yank call setreg('"', l:save_reg, l:save_regmode) let &clipboard = l:save_clipboard 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.