71

I am unable to copy from vim to system clipboard. Viceversa works, copying from outside vim and pasting with p is ok.

I have installed clipboard support, vim version is 7.3.429:

$ vim --version | grep clip
+clientserver +clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments 
+xsmp_interact +xterm_clipboard -xterm_save 

I have:

set clipboard=unnamedplus

in my .vimrc and it is set when I do:

set clipboard?

or

:echo has('clipboard')

I am using Ubuntu 12.04 LTS, and vim inside gnome-terminal (but also GVim has the same problem). Any idea?

gc5
  • 1,141
  • 4
  • 13
  • 21
  • http://stackoverflow.com/questions/11489428/how-to-make-vim-paste-from-and-copy-to-systems-clipboard This may help you out a bit. – coteyr Sep 19 '13 at 14:55
  • Did you quit vim between typing "+y on visual selection and trying to paste outside vim ? It won't work if you do – Samuel Peter Sep 23 '13 at 02:48
  • @SamuelPeter No, I didn't quit vim – gc5 Sep 23 '13 at 09:07
  • 2
    I ran into this issue because I was doing set `clipboard+=unnamedplus` something in the default setting was disabling it. Changing to `clipboard=unnamedplus` (eg removing other settings) worked for me. – JonnyRaa Jun 26 '18 at 09:01
  • 1
    As @JonnyLeeds mentioned. The config `set clipboard+=unnamed` & `set clipboard+=unnamedplus` doesn't work while using **y** and **p** for share the system clipboard. `:set clipboard?` == `clipboard=autoselect,exclude:cons\|linux,unnamed,unnamedplus`; After I removed the first `+`, which is `set clipboard=unnamed` and `set clipboard+=unnamedplus`, and it works!! `:set clipboard?` == `clipboard=unnamed,unnamedplus` – Marslo Jun 27 '18 at 15:17

5 Answers5

112

This is what works for me (Ubuntu 13.10):

Reinstall vim as vim-gtk:

sudo apt-get install vim-gtk

select what you want using the mouse - then type to copy to clipboard:

"+y

to paste to vim from clipboard type:

"+p

I don't know why but comparing the output of vim --version shows that the usual vim installation is quite restricted compared to vim-gtk. Replacing vim with vim-gtk did not affect any plugings.


Further information:

Raffael
  • 3,731
  • 7
  • 25
  • 38
  • 1
    Interestingly, on 14.04 both regular VIM and VIM-GTK have `xterm_clipboard` support, but `"+p` does not work in regular VIM. Rather than clipping to the clipboard, it increases the amont of selected lines. – dotancohen Dec 01 '14 at 10:36
  • 1
    Installing `vim-gtk` ultimately worked for me in 14.04.4, but I experienced things a bit differently than others commenting. Prior to having `vim-gtk` installed, vim was showing `-xterm_clipboard` (i.e. not enabled). After installing `vim-gtk` it shows (+xterm_clipboard). I am checking this with `vim --version | grep clip`. Anyway, makes sense that you need `+xterm_clipboard`! – overthink Jun 12 '16 at 16:19
  • Any feedback from people on Ubuntu 16.04 LTS? – Nathan majicvr.com Jun 29 '18 at 23:10
  • I confirm that this fix still works with Ubuntu 18.04. – billjoie Nov 20 '18 at 22:54
  • not work if use windows terminal to ssh to Server and code in the server – Trung Nguyen Dec 17 '21 at 10:41
7

I am going to leave the original answer below because it may be important for others that search for this question.

In short there doesn't seem to be a one size fits all answer, but with 'set clipboard=unnamedplus' one of either '*p' or '+p' should work from the "system" clipboard.

'*p' is almost certainly the one you want. (from here)


vim is a cli program. When using it inside gnome-terminal (or any terminal emulator) crtl+c (or any key combination) is handled by the terminal emulator first, then the shell, then finally by the program (vim in this case). vim and ctrl+c will almost certainly never work because ctrl+c sends an SIGINT signal to the running task. vim uses SIGINT to stop other things like aborting insert mode, or stopping search functions.

In other words. Ctrl+C is never actually passed to VIM. SIGINT is passed to VIM. SIGINT has other uses in vim so using to copy is likely not going to work (and even if can you force it, not a good idea).

Instead try sticking with Ctrl+Shift+C and Ctrl+Shift+V (there are others but I believe those put text in the system clipboard)

To be fair I don't know much about gvim.

coteyr
  • 17,948
  • 6
  • 29
  • 57
  • Maybe I confused you.. I said using `ctrl-c` OUTSIDE vim and the pasting with `p`, but in vim I know I have to use `y`.. I'll edit my question – gc5 Sep 19 '13 at 14:49
  • 1
    LOL haven't had my caffeine yet, sorry. – coteyr Sep 19 '13 at 14:52
5

In addition to the accepted answer, if you are working remotely over SSH (e.g. over tmux with multiple panes with different vim processes you want to copy between), you also need to export your X display since vim is using xterm-clipboard to interface between different processes. You can set the X display by running

export DISPLAY=:0.0

This must be run before vim is launched, and under any other shell you have.

Yusuf Gören
  • 71
  • 1
  • 6
5

Use in your vimrc:

set mouse=a

It will allow you to select and copy manually with mouse.

muru
  • 193,181
  • 53
  • 473
  • 722
Sergio Abreu
  • 204
  • 2
  • 8
4

I have found on Ubuntu 16.04 I need vim-gui-common installed in order for "+y to work.

  • 1
    Per the VIM docs, `:h registers`: _For Unix systems the clipboard is only available when the **+xterm_clipboard** feature is present._ If you look at the `vim --version` output before and after installing `vim-gui-common`, you'll see that the `xterm_clipboard` feature gets added, which is what's needed. (aside: the `vim-gui-common` package is basically installing `gvim`) – villapx Dec 11 '19 at 18:57