Linux snippets: using xclip to pipe to the system clipboard

A lot of times I write scripts to generate code, specifically in the case where I have to generate a large amount of SQL column names. If I want to then paste this into a file in the appropriate place, I can either copy and paste from the terminal (which is cumbersome, especially on Linux) or pipe it to a file, and then copy and paste it (which is also a bit unwieldy).

Instead, we can save a step by piping directly to the system (X) clipboard using xclip.  To get it on Ubuntu, we can install it from the repositories:

sudo apt-get install xclip

The default behavior of xclip is not to put its input onto the system clipboard (it puts text in the X clipboard, so you'll be able to middle click to paste in X applications, but not your IDE), so I created an alias in my .bashrc (or .zshrc) file:

alias xclip='xclip -selection c'

Then, you can pipe to the system clipboard with:

cat long_file.txt | xclip
Now you can paste the output of cat long_file.txt with the system paste command into any other application.