3

I'm using:

set clipboard=unnamed  

So that cut and copied selections go to the * register, which is conveniently linked to the x clipboard (I'm using Linux).

I would like to have it also in the + register to get both middle-mouse-pasting and Ctrl-V giving the same results in other applications. I guess this might related to the difference between xclip and xclip -selection c.

For now my workaround is:

noremap Y "+y  

So that I could do Yy to copy the current line to + register and then use Ctlr-V in some applications. But then I have to think in advance if I will use the mouse or Ctrl-V.

Is there a way I could have the + and * registers have the same value when I yank or when I select some text with the mouse?

greduan
  • 4,190
  • 4
  • 34
  • 63
e-malito
  • 767
  • 1
  • 5
  • 14
  • Maybe something along the lines of `noremap Y ygv"+y`, that would copy it to the `unnamed` clipboard and then save it to the `+` clipboard. – greduan Jan 06 '13 at 15:25
  • If that works out for you please say so, so that I can put it in an answer and you can accept it. :) – greduan Jan 06 '13 at 15:26
  • So, the funny thing, :map Y "+ygvy works but not the other way around.. do you think I could map that to y? How I can make that to work with the mouse selection as well (i.e. just selecting some text, no pressing anything, and then pasting with the two methods somewhere else? – e-malito Jan 06 '13 at 16:20
  • That's certainly funny. lol. I'll put this in an answer. :) – greduan Jan 06 '13 at 16:21

3 Answers3

3

I think you want to

:set clipboard+=unnamed,unnamedplus

From the help:

unnamedplus A variant of "unnamed" flag which uses the clipboard register '+' (|quoteplus|) instead of register '*' for all operations except yank. Yank shall copy the text into register '+' and also into '*' when "unnamed" is included.

Note that you need a fairly new Vim 7.3 (patchlevel 151?) for this option value.

Ingo Karkat
  • 154,018
  • 15
  • 205
  • 275
  • Wow! Nice one Ingo! @elmanuelito would probably go for this one if it works. :) – greduan Jan 07 '13 at 01:29
  • @ingo karkat, I'm sorry, it does not seem to work for me, I probably do it wrong. I do a simple yy or dd or x, then :reg and "+ and "* are different. My .vimrc is empty except for your command. --- My vim version: Vi IMproved 7.3 (2010 Aug 15, compiled Nov 22 2012 15:42:28) Included patches: 1-547 --- And here's my clipboard variable: clipboard=autoselect,exclude:cons\|linux,unnamed,unnamedplus – e-malito Jan 07 '13 at 18:27
  • My vim is probably too old, do you confirm? – e-malito Jan 07 '13 at 18:31
  • 1
    The version should be fine, but the `exclude:...` part must come last, as stated in the help! – Ingo Karkat Jan 08 '13 at 01:09
  • Thank you! The exclude was indeed badly placed. I didn't find unnamed plus and exclude in the clipboard help.. So far, it seems to do what I wanted, so I'll accept! – e-malito Jan 31 '13 at 11:29
1

As discussed in the comments, here's a solution:

vnoremap Y ygv"+y

However that does not work, only the other way around:

vnoremap Y "+ygvy

Here's what it does:

noremap Y: Map to the Y key, using default behavior of keybindings. :h noremap
"+y: Yank into + register.
gv: Re-select previous visual selection.
y: A normal yank into the * register (in Linux).

Glad I was able to help you solve it. :)

greduan
  • 4,190
  • 4
  • 34
  • 63
  • @elmanuelito Remember to accept the answer, that way this question actually gets closed. :) – greduan Jan 06 '13 at 16:37
  • Hey Eduan, I'm sorry, but after some testing, it's not as expected. I'd really like to have always those two registers equal. Something like mapping "y" to a vim function that does that. with my previous workaround I could do "Yy" or "Yi}". With your solution, it works with selection only. Sorry I didn't see that before. And the must would be to do that with "y" instead of "Y", and to have it work also with mouse selection only... – e-malito Jan 06 '13 at 18:49
  • @elmanuelito If it only works with selections then it might be worth adding a `v` before the `noremap`, I edited the answer to reflect that. Later when I have access to a computer with Vim I'll try to bake a simple function that does this, in the meanwhile you can use your previous solution. :) – greduan Jan 06 '13 at 18:52
  • @elmanuelito I just need a little info. I suppose you would use `y` correct? – greduan Jan 06 '13 at 19:07
  • My main use are: yy , yi} and last, make a selection and then press y – e-malito Jan 07 '13 at 18:40
1

Thinking out of the box, you can install autocutsel which sinchronizes both clipboards.

romainl
  • 158,436
  • 18
  • 236
  • 264
  • That seems like a good idea! Although I think if we need to add an extra application it might be a little less useful, know what I mean? – greduan Jan 06 '13 at 18:28
  • Hey romainl, I'm not against the idea of an external app, but I tried it without success. On top of vim, I tried with geany. "autocutsel -s PRIMARY" seemed to propagate the Ctrl-C selection to the "Mouse register" and the "Ctrl-V" one. But the opposite didn't work. With vim, something yanked would worked when pasted with the mouse, but not with some ctrl-V (or right click paste) in some other app. I tried "autocutsel" without options, without success as well. I closed the X-apps in between, but not the x-server – e-malito Jan 06 '13 at 19:01
  • @eduan, `autocutsel`, or rather the syncing feature it provides, can bu useful everywhere. I use a lot of programs besides Vim and having all "pasting" methods paste the same thing sure is useful. – romainl Jan 06 '13 at 19:25
  • @romainl Ah I see. Well I think I'll definitelly check it out then. :) – greduan Jan 06 '13 at 19:27
  • Hmm, sorry. I've had it running for a long time and I thought that it worked as advertised, which it doesn't. But I had another program running, that does that in a reliable manner: Glippy. Also, `set clipboard^=unnamedplus` is supposed to do what you want but it's not available in every build so YMMV. – romainl Jan 06 '13 at 20:43