DEV Community

Cover image for Copying files to clipboard without dark magic
Gabriel Cruz (he/him)
Gabriel Cruz (he/him)

Posted on • Updated on

Copying files to clipboard without dark magic

I've been using Linux and, more importantly, the command line for about five years now (ever since I got into Computer Science school). It's great, really. I love the command line with all my heart: it's simple, direct and fast. But there's some things you just can't do as in a GUI.

One negligible, annoying problem

Lately, I've been particularly busy doing networking stuff like copying (public) SSH keys from one machine and pasting them to the authorized_keys file of another.

If you're not very familiar with SSH or keys in general, don't worry, just picture a directory like this on my local machine:

.ssh
├── config
├── id_rsa
├── id_rsa.pub
└── known_hosts
Enter fullscreen mode Exit fullscreen mode

And another one on a remote machine like this:

.ssh
├── authorized_keys
└── known_hosts
Enter fullscreen mode Exit fullscreen mode

What I want to do is copy the contents of id_rsa.pub and append them to authorized_keys on the remote machine.

The solutions you don't want but will end up going for

  1. cat id_rsa.pub. Select the contents using a mouse (when you can). Ctrl+Shift+C (to copy). Go to the remote machine. vim authorized_keys. Ctrl+Shift+V. Paste.

  2. scp id_rsa.pub remote_host_ip:/home/your_user/.ssh/mykey.pub (copy the id_rsa.pub file to the remote machine). Go to the remote machine. cat mykey.pub >> authorized_keys (append the contents of the recently-copied mykey.pub file to authorized_keys). rm mykey.pub.

  3. You use xclip. The clipboard command-line interface for the X desktop environment. It does exactly what you want, only that's completely cryptic and, like tar, you'll never really remember that goddamn command and you'll have to google it every. single. time. For the sake of completeness here's one of the ways of doing this:

xclip -sel clip < ~/path/to/file.txt
Enter fullscreen mode Exit fullscreen mode

What would you expect from xclip instead? Probably something like this:

xclip /path/ti/file.txt
Enter fullscreen mode Exit fullscreen mode

Oh gee, I wonder if anyone ever make something like tha-

The solution you really want

GitHub logo gmelodie / xcreep

📎 Simple wrapper to xclip in Go

With xcreep you can simply

xcreep /path/to/file.txt
Enter fullscreen mode Exit fullscreen mode

And boom! It's on your clipboard.

Stupid right? I thought so too, and I've been using it daily for the past three months now. It's by far the most useful piece of code I've ever written.

Don't want to install a Go script? There's also a shell script for it!

Discussion (0)