DEV Community

Cover image for Copying files to clipboard without dark magic

Copying files to clipboard without dark magic

gmelodie profile image Gabriel Cruz (he/him) Updated on ・2 min read

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:

├── config
├── id_rsa
└── known_hosts

And another one on a remote machine like this:

├── authorized_keys
└── known_hosts

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

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

  1. cat 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 remote_host_ip:/home/your_user/.ssh/ (copy the file to the remote machine). Go to the remote machine. cat >> authorized_keys (append the contents of the recently-copied file to authorized_keys). rm

  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

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

xclip /path/ti/file.txt

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

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!


Editor guide