2

I've been looking for at way to copy from vim to my system clipboard. I've read through the question "How to copy to clipboard in vim" and from that I can see that i'll have to compile Vim with +clipboard.

This is not an option for me, when working on a remote server, so for now, I'll have to switch to the mouse, to make the selection to make my system regard the selection a selection. Simply doing a visual select through Vim does not work.

So my question is this; Is it possible to make my system see the selection made by Vim (e.g. using viw) as something that can be copied to the clipboard using a cmd-c?

I'm using Mac OS High Sierra, running iTerm 2.

Fizk
  • 1,015
  • 1
  • 7
  • 19

2 Answers2

2

I have a car without wheels, can it drive anyway? Well, the answer is no, unless you strap some sort of wheels or similar to it.

xclip may be a solution (:'<,'>w !xclip -i) but if you can install stuff, it would be easier to install a proper version of vim.

Doktor OSwaldo
  • 5,732
  • 20
  • 41
  • Maybe a $HOME local xclip version or any python script that can handle the system clipboard. There is an example from Derek Wyatt on which he shows an interesting way of dealing with external commands -> http://derekwyatt.org/vim/tutorials/advanced/#expression-register – SergioAraujo Aug 27 '18 at 17:42
  • 1
    @SergioAraujo A local version of xclip should be possible, as a local version of vim should be. The python approach is interesting, but in my short (and I mean really short, could be wrong) research it seemed like python also needs some sort of tool to access the clipboard (or at least a library) source: https://stackoverflow.com/questions/11063458/python-script-to-copy-text-to-clipboard – Doktor OSwaldo Aug 28 '18 at 05:47
  • Well, if he's on a remote server as he said, and he runs xclip, won't that copy it to the clipboard of the remote server? – ChatterOne Aug 28 '18 at 09:04
  • @ChatterOne that's true. The central part I try to answer is the "vim without clipboard support" part, not the remote. And my answer is: "you need some sort of tool/script to handel that". Your approach seems to be such a script and tool(iterm). So my answer still seems to be correct, even though yours is way more promising and if it works more helpful and therefore better. – Doktor OSwaldo Aug 28 '18 at 09:34
1

The idea of using xclip will only work if you're on a local machine.

If you're on a remote server and use xclip, it will copy the text to the remote clipboard, making it unavailable locally.

There are ways around this, and you can adapt them from this post, which can be summed up in either:

  • Use and ANSI escape sequence

OR

  • Setup communication with the server and an SSH tunnel (yes, not complicated but quite involved)

The first way is actually quite easy, because you just need to enable access to the clipboard from the terminal in the general preferences of iTerm, and then if you do

printf "\033]52;c;$(printf "%s" "YOUR_CLIPBOARD" | base64)\a"

then the text YOUR_CLIPBOARD will be copied in your local clipboard, even if you're on a remote server.

All you have to do is put that printf in a shell script that take what you want as input and map whatever shortcut you want to running the script with the selection as a parameter.

ChatterOne
  • 3,381
  • 1
  • 18
  • 24