I often use /dev/clipboard to bridge the gap between GUI tools and line-oriented tools. For example, if you're wondering how many words are in a section of text in a document you're looking at, you can put that text on the clipboard and then do this:
wc -w /dev/clipboardIf you want to insert the output of ls into a document you're editing, send it to the clipboard,
ls *.java > /dev/clipboardand then paste it in. (Yes, this task is simpler with Emacs, vi, et al.!)
You can save some typing by symlinking /cb to /dev/clipboard:
ln -s /dev/clipboard /cbSome non-Cygwin programs don't interact well with /dev/clipboard. For example, directing the output of javap to the clipboard leaves it unchanged:
% date > /cbTo compensate, I wrote a trivial script, tocb:
% javap Hello >/cb
% cat /cb
Sun Mar 30 15:23:24 USMST 2008
% cat tocbInstead of redirecting output to /cb, I pipe into tocb:
cat >/dev/clipboard
% javap Hello | tocbI also wrote a trivial counterpart for tocb named fromcb:
% cat /cb
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
}
% cat fromcbIncidentally, Mac OS X has similar commands named pbcopy and pbpaste.
cat /dev/clipboard
I got to wondering if a single script, call it "cb", could act like fromcb when on the left end of a pipeline, and act like tocb on the right end of a pipeline. Like this:
% cal | cb # puts output of cal onto the clipboardCygwin supports the isatty(file_descriptor) library function, which queries whether the specified file descriptor is connected to a terminal. (Some programs, ls to name one, change their behavior depending on whether they're writing to a terminal.) Linux has isatty(1), which simply wraps isatty(3) in a program. My Cygwin installation doesn't have isatty(1), and I was too lazy to see if it's in some package I haven't installed, but it's trivial to write:
% cb | wc # puts the clipboard contents onto standard output
% cat isatty.cWith that in hand it's easy to write a cb that that reads or writes the clipboard depending on which end of a pipeline it's on. Here it is:
main(int argc, char **argv)
{
exit(!isatty(atoi(argv[1])));
}
% cat cbUsage:
if isatty 0
then
cat /dev/clipboard
else
cat >/dev/clipboard
fi
% cal | cbYou can put cb on both ends of a pipeline to transform the clipboard contents:
% cb | cat -n
1 March 2008
2 Su Mo Tu We Th Fr Sa
3 1
4 2 3 4 5 6 7 8
5 9 10 11 12 13 14 15
6 16 17 18 19 20 21 22
7 23 24 25 26 27 28 29
8 30 31
% date | cbp.s.
% cb | tr a-z A-Z | cb
% cb
SUN MAR 30 16:03:17 USMST 2008
Maybe there is nothing new under the sun -- I just now noticed that xsel (c. 2001) apparently uses isatty to determine whether it should fetch or set clipboard contents. :)
8 comments:
Thanks for this tip.
Here's my script for accessing the clipboard (functionally identical to yours):
#! /bin/sh
isatty 0 && cat >/dev/clipboard || cat /dev/clipboard
Whoops, I pasted an older version that was using a bugged isatty.exe.
It should read as:
isatty 0 && cat /dev/clipboard || cat >/dev/clipboard
Definitely worth mentioning!
I knew there was a way to access the clipboard from Cygwin but forgot the details. You provided that and much more.
Cheers!
Nice tip!
Make it even more portable using test command instead of isatty:
test -t 0 && cat /dev/clipboard || cat >/dev/clipboard
Regards
Totally worth mentioning! I've been looking how to do this forever, but all I've run into was the clip command.. but i wanted to go both ways. How many times at work have I copied work data and wanted to awk on it! I used to have to gvim **whatever**, paste the contents, then run awk on the file. This is waaaaaaay more efficient!
your isatty(1) implementation segfaults if argv[1] isnt passed to the program (i have one exactly like it but with the extra two lines (one if, one fail-exit))
Greg's 'test -t 0' works great as a replacement for isatty.
Greg: Thanks!
Hey William,
Do you know clear this Cygwin clipboard, cause if this doesn't have any clear-cut GitBash or CMD command(one-liner) to clear the clipboard then I gotta say: This is one crappy clipboard...
Post a Comment