Two little known Mac OS X commands available from the Unix shell ("Terminal") are pbpaste and pbcopy. They are used for retrieving and sending text to the paste buffer. pbpaste outputs the cut&paste buffer to stdout. phbcopy reads stdin and puts it into the cut&paste buffer.
The more I use these commands the more new ways I find to use them. Read on.
Basic cutting and pasting:
Let's start with the basics.
Reading and writing the cut&paste buffer:To store something, just send it to pbcopy's stdin.
Easy. Right? Now go into TextEdit or MS-Word and paste (CMD-V). The file gets pasted. It's much more accurate than selecting the file from the screen. No worries about missing the first or last line, etc.
Now do the opposite. Select a bunch of text in your favorite application and copy it into your cut&paste buffer. (CMD-C) Want to save it to a file?
pbpaste >newfile.txtThe entire buffer is there. Check it and see.
Paste pipeline into buffer:
Ever been working in the Unix shell and wanted to take the output of one command and paste it into an application? (For example, paste into MS-Word for a book you are writing). That becomes a lot easier:
ls ????-*-IRR.txt | pbcopyNow the output goes into the paste buffer.
If we want to see the output too, we can either run the command twice (once with the "|pbcopy" and once without) or we can simply add a "tee" command:
ls ????-*-IRR.txt | tee /dev/tty | pbcopy
Here's a longer command line:
ls | tr '[A-Z]' '[a-z']' | sort | pbcopyWhatever you do on the command line, you can put the output into the cut&paste buffer by ending the line with | pbcopy.
Paste into a pipeline:
What about the other way? Ever have some really great stuff in your cut&paste buffer but wish you could see it sorted?
pbpaste | sortHere's a better one. How many times have you been looking at a huge Cisco router configuration and wished you could find all references to a certain IP address? I used to copy the configuration into my cut&paste buffer then paste it into NotePad or TextEdit, and use "find". However, sometimes I needed to do a complicate search that could only be done with egrep. Then I had to save the file and grep from the command line. Now its a lot more simple:
pbpaste | grep 10.1.10.1or find 10.1.10.1 as a whole word, don't treat "." as a wildcard:
pbpaste | grep -F -w 10.1.10.1or maybe we just want to count the number of lines:
pbpaste | grep -F -w 10.1.10.1 | wc -l
"sed" your cut&paste buffer:
Here's my favorite. Today I had the following string in my cut&paste buffer:
access-list 101 permit tcp object-group OSXservers any eq ftpThis is a rule for access list 101 on my PIX firewall. Now what I wanted to do is paste that into access list 202. Sure, I could retype it. I could even paste it without the ENTER at the end of the line, and use my cursor keys to get to to "101" and change it to "202". However, that's a pain and it is also error prone, epecially considering the way a PIX can mishandle command line editing on long lines leaving you without a clue about the real location of your cursor.
Therefore I did this instead:
pbpaste | sed -e 's/ 101 / 202 /g' | pbcopyTo test my work, I looked at the buffer:
$ pbpasteIt worked! W00t! Thus I pasted it into my PIX and was on my way.
access-list 202 permit tcp object-group OSXservers any eq ftp
The system is smart enough to tell ASCII text from RTF from EPS. The man page has more information.