Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
On the Mac you're probably with the concept of the Clipboard. You're inside a document in one application, you select some text, you copy it, you switch to another document or another application, and you paste it there. Well, in Unix we don't have a global clipboard like that. But we can make use of the same one that the Mac Finder uses. And we can use it within Unix or we can use it to ferry data back and forth between Unix and the Finder. To do it we'll use a pair of Mac-only Unix commands called pbcopy and pbpaste. The pb stands for Pasteboard. Now when you're going to select something and copy it in the Finder, you use your mouse to select it.
But in Unix we don't have a mouse. I mean of course I have the mouse here. That's the Mac OS allowing me to work with Terminal. But in Unix itself, it's all command line. So in order to select something we're going to need to have input. And we've already seen how to work with input in Unix. So let's say for example that I hit ls -lah. If I just hit Return, that'll go to standard out. But instead what I can do is pipe that output as input into pbcopy. Now that same text that would've been output is on my Clipboard.
We can see that if we just switch into TextMate real quick. We'll open a new document and paste it in there. This can be really handy when we're working in Unix and let's say we're trying to pull out a couple of columns out of our us_presidents tab separated values file. Well, we can find everything that we want. Now we certainly could select this and then we could scroll back up the page, but that might be a lot. Instead, a much simpler way would just be to simply take that and send it to pbcopy. Now without any output and without having to switch and use our mouse to copy the text, we've got it on our Clipboard. We can switch to the Finder and we can paste it in there.
And we can use our other techniques for directing input, so pbcopy and let's pipe in our lorem_ipsum.txt file. Now the entire contents of that file are on the Clipboard. We didn't open it, we didn't have to scroll up at the Terminal output and select it and then copy it, fill up our Terminal window. All we did was take the contents of the file, put it on our Clipboard so we're ready to do something with it. Now the companion to pbcopy is pbpaste. So we just do pbpaste by itself. It outputs whatever is on the Clipboard to the standard out. So there we are.
We see it in the Terminal window. We can instead direct that output. We can for example, send it to a file, so clipboard.txt. Now that content is in a file. One nice trick that I like to use with directing output from pbpaste is to create an alias. I'll call it pbsort and from that what we'll do is we'll say take whatever is on the Clipboard and paste it, pipe it into sort, and then pipe the results of that back onto the Clipboard. Replace what's on the Clipboard essentially with now the new sorted version. Now remember, aliases that you want to keep around you'll need to put in your bashrc file.
But this will serve our purposes. Let's say that I have a list: monkey, zebra, lion, tiger, and bear. I'll take that, send it in to translate to convert the commas into new lines so each one will get its own line, and then I'll put it on my Clipboard. Now I'll just call my pbsort and now it's sorted. pbpaste, you can see it, or we can switch into another document somewhere else and paste it in there. Now the original list doesn't have to start in Unix either. We could, for example, copy this, put it on our Clipboard, switch over to Unix, and regardless of what directory we are in, we just type pbsort. Boom! My Clipboard is now sorted.
We come back into whatever application we were working in and we paste the sorted version back in. The last trick that I want to show you is how to use multiple clipboards. Have you ever wished that you had more than one clipboard when you were working on the Mac? Well, it may surprise you to learn that Mac OS X actually has four clipboards. It has the general Clipboard which we've been using here and then it has one called find, one called font, and one called ruler. The find one you may have bumped into before because it's whenever you do a find in something like your web browser for example. You do a find there, it puts it on the Clipboard, and then when you switch to another application, you do a find there, you may see that same value pop up, and you may have wondered how did it get that, how did it have that same value that was in that other application? Well, it's because it was on the Find Clipboard.
From Unix we can actually directly access these. So for example, let's say we have First, and we'll send that to pbcopy, and then we'll specify the clipboard we want with pboard followed by either the words general, font, find, or ruler. So general is the one we've been using. Let's now echo "Second" and let's pipe this to pbcopy, but this time the Pasteboard that we'll use will be the find. Now if we just say pbpaste, which one will we get? Well, we get the contents of the general one.
But if we say pbpaste with pboard find, now we get the contents of the second one. It can be a really handy way for you to juggle data. If there is something on your Clipboard that you don't want to lose, well, you can just switch it to another Clipboard. Just pipe it off of one Clipboard onto the Ruler Clipboard let's say, and then go ahead and do the regular copy/paste you need. When you're done you can issue another command that will pipe it back from the Ruler Clipboard onto your general Clipboard and you won't lose the data. I think you'll find that using pbcopy and pbpaste will really enhance the way you work with the Clipboard.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.