Fizk
Fizk

Reputation: 1114

Possible to use Cmd+c to copy vim selection?

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.

Upvotes: 2

Views: 1440

Answers (2)

ChatterOne
ChatterOne

Reputation: 3541

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.

Upvotes: 1

B.G.
B.G.

Reputation: 6016

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.

Upvotes: 2

Related Questions