Sometimes you just want to copy something from your terminal and paste it somewhere else. You might have heard of a Linux program called xclip , which provides a command line interface to X selections. However, xclip ’s default selection isn’t the clipboard, and typing xclip -selection c -i ~/.ssh/id_rsa.pub is just a bit tedious.

So here’s a wrapper function that makes it less of a hassle to integrate the clipboard with the command line.

It handles input via pipe or parameters.

It automatically uses the contents of a file if you pass it a valid filename.

It prints an excerpt of what has been copied, truncated to 80 characters.

Examples

Pipe anything to the clipboard

$ tail -n 100 /var/log/apache2/error.log | cb # = > Copied to clipboard: [ Sun Oct 02 08 :02:08 2011 ] [ notice ] Apache/2.2.17 ( Ubuntu ) configured -- resumin...

Copy the contents of a file to the clipboard

$ cbf ~/.ssh/id_rsa.pub # = > Copied to clipboard: ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAnwaNIuOhZzUeR6/xEEudXt3zEh91dawhkkKx8p/+4Bw9...

Type straight into the clipboard

$ cb This is some unquoted text. # = > Copied to clipboard: This is some unquoted text.

No options, no man pages.

It also comes with a handy cb_ssh alias that copies your SSH public key to the clipboard, for when you are setting up your new BitBucket account with unlimited, free private git repositories! I’m not affiliated with Atlassian, I just think they’re awesome.

So if you think this looks handy, you can add the following to your ~/.bashrc :