Monday, April 21, 2008

xclip: A Cut-and-Paste Aid

I'm forever cutting and pasting, snarfing-and-barfing, Jake Edge calls it. What's that you say? You too?

Do you know about xclip? I didn't.

I just saw a post somewhere that suggested using
cat big-file-name | pbcopy
to put a large file into the clipboard. "Ooooh!" I thought. "A command that captures stdin and slaps it into the clipboard? I could use that for a lot of things."

First, I tried apt-get install pbcopy. No such package. How about apt-cache search pbcopy? Nothing.

When I Googled "pbcopy ubuntu" I learned why: pbcopy is an OS/X tool. For Ubuntu, the analogue is xclip. (And sudo apt-get install xclip works.)

By default, xclip puts its input into the X Windows clip buffer (duh), but I've also defined an alias to get me used to using pbcopy:
alias pbcopy='xclip -selection clipboard'
I've found that if I have a work-alike to a command in another distro, making an alias with the same name is a good didactic tool; it helps me get used to the command and makes it portable. Your mileage may vary.

So, fabulous stuff. I'll just write this post in vi, cat it into pbcopy, and use ^V to paste it into Blogger. Um. No. Neither that, nor a simple xclip seems to work. I can paste it into a text file, but not into blogger.

More investigation is needed.

Update:

Ah. xsel (from the package of the same name) works much better.

1 comment:

Jeff Shipley said...

xclip, by default, places text into the selection buffer. This is a little different from the clipboard.

Pasting from a selection is as easy as middle-clicking your mouse. This makes it really easy to copy-paste, since all you have to do is select something (with the mouse or keyboard) and then middle-click.

If you want xclip to put the text into the clipboard (the buffer that requires explicit copy/paste commands to use) then call
xclip -selection "clipboard"

This is actually a pretty useful feature of xclip, not an issue. I use highlight/middle-click to copy and paste all the time.