Ssh – Copy input to clipboard over SSH

clipboardsshxorg

Here's my usage case:

  • I'm often connected to other computers over SSH for work and I often need to copy and paste documents/text from the server to locally running editors for writing examples and sharing text.
  • Often, if the text is small enough, I'll simply copy the output from my terminal program (gnome-terminal at the moment) and paste it.
  • However, when it comes to entire documents, my options are quite limited. I can either copy the document chunk-by-chunk, or scp it to the local machine.

Is there a way to use a program such as xclip which will allow me to copy remote stdin to the local X server's clipboard? Something to the effect of:

cat myconffile.conf | sed {...} | copy-over-ssh-to-local-clipboard

would be awesome. Does something exist to make this possible?

Best Answer

If you run ssh with X forwarding, this is transparent: remote commands (including xclip) have access to your X server (including its keyboard). Make sure you have ForwardX11 yes in your ~/.ssh/config and X11Forwarding yes in the server sshd_config (depending on your distributions, these options may be on or off by default).

<myconffile.conf sed {...} | xclip -i

There are other ways of working on remote files that may be more convenient, for example mounting remote directories on your local machine with SSHfs, or opening remote files in Emacs with Tramp. If you have ssh and FUSE set up and SSHfs installed, SSHfs is as easy as mkdir ~/net/myserver; sshfs myserver:/ ~/net/myserver. If you have ssh set up and Emacs installed, Tramp is as easy as opening /myserver:/path/to/file.

Related Question