Vim: Access to System Clipboard via Ssh - Linux to Os X

Vim: access to system clipboard via ssh - Linux to OS X

Basically, you can just open remote file on your linux machine like:

:e scp://user@host/relative/path/from/home.txt

Another solution to copy directly from ssh session would be X11 forwarding in ssh which connects system clipboard between remote and local machine.

  1. Enable X11Forwarding on the SSH server side in /etc/ssh/sshd.conf

  2. Use the -Y option for ssh client to enable it when connecting:
    ssh -Y your_server

Now you can copy in the remote Vim using "*yy and paste it locally in Vim using "*p or paste it in any GUI app using Ctrl-V.

vim + COPY + mac over SSH

To expand on Ray's answer…

When you are using Vim on a remote server via SSH, everything you do in Vim is done on the remote server. The remote server and the remote Vim that you are running on it have zero practical knowledge of your local computer and its system clipboard.

Because of that, y will never put the yanked text in your local clipboard.

In order to copy a chunk of text from the remote Vim to your local machine's clipboard you have three options:

  • Select the text with your mouse and hit Cmd+C like in any Mac OS X application.

    Obviously, it seems to be the easiest but it has at least three limitations:

    1. It is limited to the current screen. If the text you want to yank is not displayed entirely you won't be able to copy all of it.

    2. It doesn't play well with set mouse=a. With this option, any attempt to select something with the mouse will result in a visual mode selection which can't be copied with Cmd+C. As a workaround, you can use Alt+mouse to select the text without entering visual mode or simply remove this setting from your remote ~/.vimrc.

    3. Line numbers are copied as well.

  • Put the yanked text in a temporary file, scp it to your local machine and use pbcopy to put it in your system clipboard.

    This solution seems to be a little convoluted but it works (and the problem itself is also a little bit convoluted). Over the years I've seen a lot of different implementations ranging from simple one liners to client/server setups. Here is one, feel free to google around for others.

  • Use X-forwarding to connect your local clipboard to the remote clipboard if available.

Vim copy clipboard between mac and ubuntu over ssh

Here is the content of one of my gists:

Clipboard sharing on Mac OS X is easy.

On the Mac

  1. Install or update XQuartz.app and start it.

  2. In the Preferences window, activate clipboard synchronization.

  3. Quit XQuartz.app.

  4. In iTerm.app or Terminal.app, connect to your remote machine with:

    $ ssh -X username@host

    and see the XQuartz.app icon pop-up in your Dock.

From now on, XQuartz.app will start automatically in the background when you use the -X flag, taking care of the clipboard synchronization for you.

On the remote machine

  1. If you don't already have it, install GVim. On Debian-based systems, use:

    $ sudo apt-get install vim-gtk

    The idea is not to use Gvim but installing it gets you everything you need to get clipboard sharing to work:

    • a minimal X
    • a Vim built with clipboard support
  2. In Vim, synchronize the unnamed and clipboard registers by adding this line to ~/.vimrc:

    set clipboard^=unnamed

How to make vim paste from (and copy to) system's clipboard?

Be aware that copying/pasting from the system clipboard will not work if :echo has('clipboard') returns 0. In this case, vim is not compiled with the +clipboard feature and you'll have to install a different version or recompile it. Some linux distros supply a minimal vim installation by default, but if you install the vim-gtk or vim-gtk3 package you can get the extra features nonetheless.

The "* and "+ registers are for the system's clipboard (:help registers). Depending on your system, they may do different things. For instance, on systems that don't use X11 like OSX or Windows, the "* register is used to read and write to the system clipboard. On X11 systems both registers can be used. See :help x11-selection for more details, but basically the "* is analogous to X11's _PRIMARY_ selection (which usually copies things you select with the mouse and pastes with the middle mouse button) and "+ is analogous to X11's _CLIPBOARD_ selection (which is the clipboard proper).

If all that went over your head, try using "*yy or "+yy to copy a line to your system's clipboard. Assuming you have the appropriate compile options, one or the other should work.

You might like to remap this to something more convenient for you. For example, you could put vnoremap <C-c> "*y in your ~/.vimrc so that you can visually select and press Ctrl+c to yank to your system's clipboard.

You also may want to have a look at the 'clipboard' option described in :help cb. In this case you can :set clipboard=unnamed or :set clipboard=unnamedplus to make all yanking/deleting operations automatically copy to the system clipboard. This could be an inconvenience in some cases where you are storing something else in the clipboard as it will override it.

To paste you can use "+p or "*p (again, depending on your system and/or desired selection) or you can map these to something else. I type them explicitly, but I often find myself in insert mode. If you're in insert mode you can still paste them with proper indentation by using <C-r><C-p>* or <C-r><C-p>+. See :help i_CTRL-R_CTRL-P.

It's also worth mentioning vim's paste option (:help paste). This puts vim into a special "paste mode" that disables several other options, allowing you to easily paste into vim using your terminal emulator's or multiplexer's familiar paste shortcut. (Simply type :set paste to enable it, paste your content and then type :set nopaste to disable it.) Alternatively, you can use the pastetoggle option to set a keycode that toggles the mode (:help pastetoggle).

I recommend using registers instead of these options, but if they are still too scary, this can be a convenient workaround while you're perfecting your vim chops.

See :help clipboard for more detailed information.

vim select buffer to clipboard via putty ssh connection

You can select the register in witch you can copy anything. The list of available registers is available through :registers. The clipboard register is usualy "*. Therefore if you want to copy anything in this register, just prefix the yank command by the register, ie: "*y. The steps would be like :

  • Enter visual mode (Shift-v for instance`)
  • Select your lines (mouvement)
  • Type "*y

To make the copy work with putty, you'll have to enable X-forwarding, vf Copy from Putty/Vim visual mode to windows clipboard

How to send data to local clipboard from a remote SSH session

I'm resurrecting this thread because I've been looking for the same kind of solution, and I've found one that works for me. It's a minor modification to a suggestion from OSX Daily.

In my case, I use Terminal on my local OSX machine to connect to a linux server via SSH. Like the OP, I wanted to be able to transfer small bits of text from terminal to my local clipboard, using only the keyboard.

The essence of the solution:

commandThatMakesOutput | ssh desktop pbcopy

When run in an ssh session to a remote computer, this command takes the output of commandThatMakesOutput (e.g. ls, pwd) and pipes the output to the clipboard of the local computer (the name or IP of "desktop"). In other words, it uses nested ssh: you're connected to the remote computer via one ssh session, you execute the command there, and the remote computer connects to your desktop via a different ssh session and puts the text to your clipboard.

It requires your desktop to be configured as an ssh server (which I leave to you and google). It's much easier if you've set up ssh keys to facilitate fast ssh usage, preferably using a per-session passphrase, or whatever your security needs require.

Other examples:

ls  | ssh desktopIpAddress pbcopy
pwd | ssh desktopIpAddress pbcopy

For convenience, I've created a bash file to shorten the text required after the pipe:

#!/bin/bash
ssh desktop pbcopy

In my case, i'm using a specially named key

I saved it with the file name cb (my mnemonic (ClipBoard). Put the script somewhere in your path, make it executable and voila:

ls | cb

System Clipboard Vim within TMUX within SSH session

You need to do two things.

  1. On your remote system, install a clipboard-aware Vim (and the X dependencies needed for clipboard support):

    $ sudo apt-get install vim-gtk
  2. On your local system, start your ssh session with X11 forwarding enabled:

    $ ssh -X user@hostname

    See $ man ssh for the security implications of X11 forwarding.



Related Topics



Leave a reply



Submit