Archive for February, 2008

xclip – Use the Clipboard From the Command Line

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).

Leave a Comment

Getting Back To Basics

While Ubuntu does a very good job providing graphical alternatives to traditional console applications and utilities, I remain a staunch proponent of the console, as those who know me can attest. I could even go as far as saying that I’m a “zealous proponent” of the console, but I do use plenty of graphical applications, whether for aesthetics or for ease of use or for functionality. I don’t particularly favor one or the other.

However, in many cases, the console is much, much faster if you know what you’re doing. A friend of mine said to me just tonight, “you have a tendency to insist CLI is better just because you’ve spent the time learning how to use it.” He’s exactly right. I’ve spent countless hours reading man pages, howtos, tutorials and change logs because I realized early on in my UNIX-like OS “career,” that the console could open up some very powerful opportunities for me if I only took the time to learn how to use it effectively.

Older hardware can also benefit immensely from a console-heavy environment, although it need not be entirely text-based. But when you have a 300MHz Pentium II with 128MB RAM you probably don’t want to run a full GNOME desktop, Azureus and Amarok. But with command line utilities and a bit of know-how you can still use that machine very effectively (I even had good success selling PII 300MHz with 128MB RAM and Windows XP as “web/email laptops” in 2004, so imagine what you can do with a bit of Linux console magic!) .

Some of my favorite console applications, both old and new, include irssi, the IRC client I absolutely cannot live without, rtorrent, mplayer, screen (which is incredibly powerful and useful when combined with other CLI apps), finch and, of course, ssh. We’ll explore these and others in a series of article I intended to write and I’ll do my best to provide links to the wealth of other sites that provide much better articles than I ever could. I hope to learn a lot more myself during the process, so you won’t  be going it alone!

I am not a console expert. I’ve only been using Ubuntu for just over 16 months, although I did mess around with several versions of Red Hat in the late 1990s, when Linux did not support internal modems much at all, and I didn’t own an external serial modem. Somewhat ironically, I became reintroduced to the console courtesy of Mac OS X which I began using in 2005 and by the time I began using Linux again in earnest I was hooked. Now I know enough to get around in the console of most Linux distribution, BSD and Solaris. But I have much, much more to learn.

So stay tuned for several updates per week in which I investigate new console applications and utilities. You’ll be a “console snob” in no time! 😛

Leave a Comment