Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in vim with xclip, yank to clipboard

Tags:

vim

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!

like image 492
Nyambaa Avatar asked Mar 09 '11 01:03

Nyambaa


People also ask

How do I enable clipboard in Vim?

To copy text to the system clipboard, use "+y . The " allows you to specify the register, + is the register that represents the system clipboard, and you already know what y does. Alternatively, to tell vim to use the + register as the default register, put set clipboard=unnamedplus in your . vimrc.

How do you copy and paste on Xclip?

How do you copy a file to the clipboard in Linux? xclip-copyfile command copies files into the X clipboard, recursing into directories. xclip-cutfile command Copy the files, but also deletes them afterwards. xclip-pastefile command Paste the files out of the clipboard.


2 Answers

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.

like image 101
ephemient Avatar answered Nov 12 '22 11:11

ephemient


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.

like image 44
Walf Avatar answered Nov 12 '22 13:11

Walf