17

I have vim 7.2 (-clipboard -xterm_clipboard ...) in Ubuntu. You can see that it's not support clipboard. So I want to write little vim script which copies visual selected text into the clipboard using xclip tool.

You know xclip tool works like that:

echo 'hello' | xclip -selection clipboard      #it copies 'hello' into clipboard

And vim can run shell commands, so I want to copy visual selected text to where instead of 'hello', But I don't know how to combine xclip and vim. Can you help me to implement it.

Thanks for your time!

Nyambaa
  • 38,988
  • 9
  • 29
  • 35

2 Answers2

38

Are you using your distribution-provided vim? If so, the vim-tiny, vim, and vim-nox packages have no clipboard support, but it does exist in vim-lesstiff, vim-gtk, and vim-gnome.

If you insist on doing it your way,

:'<,'>w !xclip

would send the current selected lines to xclip, and

:call system('xclip', @0)

would send the last yank to xclip.

ephemient
  • 198,619
  • 38
  • 280
  • 391
  • 1
    +1 for pointing out what might be the real problem (using the wrong package) in addition to the answer – Daniel DiPaolo Mar 09 '11 at 03:00
  • yeah, I'm using distribution-provided vim in Ubuntu 10.04. – Nyambaa Mar 09 '11 at 03:38
  • thanks ephemient, I tryid like that: ":'<,'>w !xclip -selection clipboard" as you said, it copies whole line into clipboard, how to copy just visual selected text not whole line? – Nyambaa Mar 09 '11 at 03:40
  • @Nyambaa: Try switching to the distribution-provided `vim-gnome` or yanking first. Handling character-wise, line-wise, and block-wise selections in vimscript is nontrivial. – ephemient Mar 09 '11 at 04:13
  • 6
    I had to make visual selection, type `:w !xclip -selection clipboard`. – Kris Aug 12 '16 at 09:48
  • Is there a way to make `gx` pass what's under the cursor to `xclip`? – Geremia Oct 07 '16 at 17:07
  • Thanks. I was looking for that kind of solution since I first tried to switch to vim-gtk but it was having trouble handling enormous log files. Maybe a better solution for me is compiling it just adding +clipboard support – drruggeri Jan 16 '18 at 20:21
  • The other comment here mentioned "make visual selection, type `:w !xclip -selection clipboard`" which works but is too long to type. The default behavior of `w !xclip` actually works, and you just need to paste the content by clicking BOTH left and right mouse bottoms simultaneously. – RayLuo Mar 05 '23 at 00:13
5

For me, Vim stopped being able to copy to the * and + registers over SSH, even though :echo has('clipboard') was 1, and other X programs still worked. The solution for me was to add a mapping that yanks (via a register) to xclip:

vnoremap <silent><Leader>y "yy <Bar> :call system('xclip', @y)<CR>

I select text, hit \y and it arrives on my local clipboard. You can change which register it uses, e.g. c for "clipboard" with "cy and @c.

Walf
  • 8,535
  • 2
  • 44
  • 59
  • Crazy thing that happens here: after doing that, I can `middle-click` to paste the yanked lines on my local computer, but it will take a while. It seems the actual fetching of the content happens at the time I click to paste, not at the time I yank. It happens that also when middle-clicking for other reasons, like closing browser tabs, also takes a while. This can be solved by just selecting other text after you're done with the remote clipboard contents. – fiatjaf Dec 30 '18 at 16:54
  • A `:vmap` command shows `v ,y * "yy | :call system('xclip', @y)`. Clipboard contents are not affected. EDIT: If I change `'xclip'` to `'xclip -sel clipboard'`, it works as expected. – Tony Jul 07 '20 at 00:57