SSH with clipboard support

This week I stopped working full time on SignDict and started a new job at Wooga. Starting a new job often means to adapt to new ways of working. One of the biggest changes for me is that from now on I am working in a VirtualBox on a Debian system. That box is setup using Vagrant and relies heavily on docker and other nice litte things.

One of the problems I had at first was the clipboard. I wanted to have a working clipboard between my Mac and the ssh sessions on the virtual machines. After tinkering for an hour I now have a working solution. I think this could be valuable to others, so here it is :wink:.

First step:

Install xorg and xsel on your Linux boxes. If you prefer to do it manually run this line:

sudo apt install xorg xsel

The clipboard will use the X11 protocol. Xsel is a nice command line util that exposes this to you.

After that install Neovim and add the following line to the config if you want to use the system clipboard for all operations:

set clipboard+=unnamedplus

Neovim uses xsel to read and write to the clipboard.

Next up start install and start XQuartz. It is important to start it before you open the ssh sessions. I have added it to my startup elements of my user on the Mac.

If you use vagrant add this line:

config.ssh.forward_x11 = true

After that you can use vagrant ssh to ssh to your box and have a working clipboard in your Neovim on your linux boxes. You can also start the ssh client manually using ssh -X user@ip to enable the X11 protocol.

You can also pipe to the clipboard using echo "hello" | xsel -b or read it with xsel -b.

Small warning: you should only enable the X11 to trusted servers. Never allow it for servers you don’t have under your own control.

Update: If you also use tmux inside of your vagrant box, you might need the tmux-yank plugin to make it all work inside of your shell.

Portrait photo of Bodo Tasche
Bodo Tasche
Polyglot Developer

I am a freelance polyglot developer and love HTML5, testing, TypeScript, Ruby and Elixir. In the last 20 years I have been in lots of different roles, from Java to Elixir, from backend developer at a 3 people team in an early phase startup to the CTO of a web agency. Some of my work can be seen on my projects page.

Need help developing your MVP or to add new features into your current app? Need a CTO or a front/backend developer for hire? Send me an email.