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!
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 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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With