1

I have discovered the joy of having :set clipboard^=unnamedplus.

One issue I often face however is having whatever was last copied or yanked be overwritten by what I just deleted.

I don't think manually referencing the blackhole ("_) register everytime is a satisfactory solution, and doing :nnoremap d "_d (or the same with x) has the major downside of preventing me from using any other buffer ever for these operations.

I thought something like

augroup yankClips
" Like `:set clipboard^=unnamedplus`, but only with the yanks
    autocmd!
    autocmd TextYankPost * if v:event['operator'] == 'y' | let @+=@" | endif
augroup END

joined with not using :set clipboard^=unnamedplus would solve my issues by only putting yanked text to the + register, but removing unnamedplus from the clipboard prevents p from putting what's inside the + register by default.

Given these informations, does anybody know of a way of having what is essentially :set clipboard^=unnamedplus without the d, x and s operations going into the + register?

2
  • Not sure about Linux, but on macOS with all the bracketed-paste stuff set up correctly, Cmd+V pastes into Vim from the clipboard kind of like like doing "*p. Though I imagine that would be Ctrl+V on Linux… Have you considered using :nnoremap <leader>p "+p?
    – D. Ben Knoble
    Apr 24 at 18:10
  • @D.BenKnoble I would like to avoid having to do Ctrl+Shit+V to paste, but it does indeed work. I have also considered having <leader>p for "+p, but I think I would rather deal with the inconvenience of a regular, vanilla :set clipboard^=unnamedplus than having to "use up" <leader>p for that (though I don't have it mapped right now, I have thought of a couple of ways it could serve me). Your suggestion is very welcome, though it is not quite what I am asking.
    – dvqc
    Apr 24 at 18:19

1 Answer 1

0

It looks like there are two angles, here: either change how y works or change how d, x, c, and s work.

Here is one way to approach the latter, which I think is the easiest of the two:

:nnoremap <expr> d v:register =~ '[\*+]' ? '""d' : 'd'

:help v:register says:

The name of the register in effect for the current normal mode command (regardless of whether that command actually used a register). Or for the currently executing normal mode mapping (use this in custom commands that take a register). If none is supplied it is the default register '"', unless 'clipboard' contains "unnamed" or "unnamedplus", then it is '*' or '+'.

So we can check if the value of v:register is * or + and…

  • if it is, then use the unnamed register explicitly, which doesn't sync with "+ or "*,
  • if it is not, then let Vim use whatever register you specified.

With such a scheme in place for d, x, c, and s, the only command that could change "+ and "* would be y.

One obvious limitation, though, is that the "+ and "* registers can't be used explicitly anymore for the mapped commands. I could live with that because I don't think I ever needed to d, x, c, or s to the system clipboard, explicitly or not, (I only use clipboard syncing for y and p) but could you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.