Copy and paste across machines with ssh + xsel

This is pretty obvious with hindsight but I only just thought of it!
I often have my work laptop on the desk next to my home desktop, and sometimes for whatever reason I need to copy & paste a URL or similar snippet between the machines.

Firstly, if you’ve never used xsel before, it’s very useful. Pipe something into it, and it becomes the current selection. Use it without input and it outputs the current selection buffer. It has a few more options, so check the man page.


grep foo ~/bar | xsel
xsel < ~/.conkyrc
xsel > ~/saved

Secondly, I strongly recommend that you set up public key authentication and ssh-agent if you haven’t already. It will make your life much easier!

Now the important part. To transfer the paste buffer from the current machine to a remote machine:

xsel | ssh -X "DISPLAY=:0.0 xsel -i"

Obviously you can replace the input with a local file or the output of a command just as easily, e.g:

ssh -X "DISPLAY=:0.0 xsel -i" < foo.html

In case you aren’t familiar with the traditional X11 paste method, this means you select something on the source machine, run the command above, and then use the middle mouse button on the remote machine to paste. If you don’t have one, use the left and right buttons simultaneously.

Hope this is useful!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: