27

I am using gVim on Ubuntu 10.10. I want to copy (yank) text to the system clipboard, so that the copied text is available in other applications.

This works with "+y. But I want to have it working with y.

I have tried to map y to "+y but then yy doesn't work anymore (since it produces "+y"+y).

I have also tried :set clipboard=unnamed but this works only the other direction: Text in the system clipboard I can paste with p.

2
  • I just use different mappings for dealing with the clipboard, and keep the normal functionality of the y command Dec 31, 2010 at 14:35
  • In case it's unclear to some, "+y means you have to type ", +, y (" is the register symbol, + is clipboard register, y is yank)
    – Wadih M.
    Feb 2, 2022 at 19:34

5 Answers 5

16

Did you try to map with this command:

noremap y "+y

? This mapping contains a serious mistake: it maps y in normal, visual and operator-pending modes, while you need only normal and visual modes. Use the following:

nnoremap y "+y
vnoremap y "+y

Also try set clipboard=unnamedplus (it requires at least vim-7.3.74). set clipboard=unnamed works in both directions, but it sets «mouse» (*) register which is different from clipboard register (+).

12

Most online solutions simply tell you to map y to "+y. But sometimes the issue is "+y doesn't even work. You do need to check what features your vim has been compiled with.

Try the steps below:

  1. Open your terminal, run vim –-version | grep xterm_clipboard
  2. Check the sign before xterm_clipboard, if it’s a + (plus sign), go to step 4.
  3. If it’s a - (minus sign), run sudo apt-get install vim-gnome, then sudo update-alternatives –config vim, select vim.gnome in the list (You should use the proper command that corresponds to your system). Run command vim –-version | grep xterm_clipboard again, now you should be able to get + (plus sign).
  4. Check whether your system clipboard uses + (plus sign) or * (star sign) register of vim, this depends on the OS you're using, sometimes they’re equivalent. How to check? Just copy some random text, then open vim and type :reg, check which register shows the string you just copied. If it’s a + (plus sign), add set clipboard=unnamedplus to your .vimrc. If it’s a * (star sign), add set clipboard=unnamed.
  5. Test it out. Copy something inside vim, and then type :reg to check if the system clipboard has changed. If it does, your will get whatever is in that register when you paste outside of vim.
2
  • @CHID glad to be of help
    – Rui Liu
    Apr 3, 2014 at 15:57
  • 1
    Worked for me, thanks. But what do the commands in step 3 do? (And why does it work?)
    – Daniel Que
    Jun 25, 2014 at 18:22
4

I have the very same idea as you, but I did it for years.

nnoremap yy yy"+yy
vnoremap y ygv"+y

Note that now yy command does two things: First it yank to register as normal, and then it yank to " register (system clipboard). The y command does the same thing. This is because I want to keep the multiple clipboard functionality of Vim.

For pasting from system clipboard, I used to have noremap gp "+p (global pasting), but now I use the excellent plugin Yankring.

2

Select some text in visual mode and it will be inserted into the system clipboard (the one where you middle-click to paste, I cannot recall the exact name).

If you set mouse=a you can use the mouse for visual selection like you would in many other applications.

1
  • For some reason, the middle-click copy of "visually-selected" text doesn't work for me in cells of a jupyter notebook in Firefox. Pasting after "+y works.
    – bli
    Mar 1, 2018 at 11:53
0

In my case, I can sometimes copy from gvim to the system clipboard and sometimes not. I found a workaround, though I don't understand the underlying problem. If I copy text in another application (e.g. Notepad, as I am on Windows 7), then I can copy text from gvim and paste it elsewhere. It looks I need do this for each copy out of gvim.

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.