Today I was setting up an account on Github and I needed to supply them with my ssh public key. The recommended method was to use something like pbcopy, which is on Mac OS X to copy the contents of STDIN to your clipboard. This was to make sure you don’t have any extra lines or spaces when you do your copy to the clipboard as public keys need to be exact.
This is where xclip comes in. xclip allows you to pipe content into your clipboard directly from the command line as well as outputting your clipboard. In terms of my example, it’s as simple as this:
cat id_rsa.pub | xclip
There’s only one problem with this example. Piping simply to xclip will only fill the clipboard you can use via xclip o itself. Not your usual CTRL+V, or Right Click>Paste board. So how do you do that?
cat id_rsa.pub | xclip -selection c
Now when I go into Firefox or anywhere else, hitting CTRL+V I get the contents of id_rsa.pub. You can do this with ANY standard input to your terminal. File listings, file diff’s, drive space, etc .. you are not very limited.
Since I never plan to use xclip -o, I made a simple alias so I don’t have to constantly type -selection c, like so:
alias xclip=’xclip -selection c’
Now whenever I use the xclip pipe like I did in the first example it goes straight to my regular clipboard.
You may need to install xclip as it does not come default with Ubuntu. The xclip manual (man xclip) has a variety of other information I did not cover in my post, but this is all I personally need from it.