Yesterday morning I had a bit of a strange question (so I thought), although it turned out to be a good one. “Is there any way to pipe or redirect output from the console to the clipboard?” I asked my good pal jdong if there was indeed a way to do this. He quickly turned me onto this Debian page where I discovered a neat little application called xclip.

You can read the nuts and bolts of its usage on the Debian page, but it’s quite easy. I’ll use an example based on what I originally wanted it for (being able to paste file listings of various directories into a pastebin or document):

ls -al /media/storage2/Comedy/* | xclip

You can retrieve what went into the “clipboard” by running xclip -o, which will paste back into the terminal whatever you copied using the example above.

Unfortunately what it did not do was save the output to the regular “X” clipboard (where items normally go when you highlight and do a Ctrl+v or Ctrl+x in the GUI or Ctrl+Shift+c/x in the console), which is what I was expecting, but this is easily remedied. In order to do that, we must run the following:

ls -al /media/storage2/Comedy/* | xclip -selection c.
(To paste from the X clipboard it’s simply a matter of running xclip -selection o, but you can also just use Ctrl+Shift+v)

xclip supports a lot of extended features such as split clipboards (which I’m still not quite sure what those are), but I mainly wanted to use it for grabbing file listings. So all I did was make an alias by running:

alias xclip='xclip -selection c'

Now when I pipe output to xclip I can quickly switch to my web browser or document, hit Ctrl+v and my console output appears in a pastebin or whatever else i’m working on. It’s quite handy for both writing technical documents and for troubleshooting. Pastebin is the darling of IRC idlers, and this just made it easier.

I didn’t bother with aliasing “xclip -selection o” simply because I can just use Ctrl+Shift+v and paste back into the console from the X clipboard as mentioned above.

Hopefully that wasn’t too confusing. Use it a few times and it becomes a lot more intuitive. I certainly like it, especially for large outputs that would exceed the scrollback buffer (I use the Awn Terminal Applet constantly, which has a small buffer).

Advertisements