macOS: Use Clipboard in Terminal Without a Mouse

2 minute read
| Quick Tip

If you do much work at all in the Terminal, there are two macOS commands you should know about but probably don’t. They’re called pbcopy and pbpaste, and they’re extremely powerful. These two commands let you use Clipboard in Terminal without ever raising your fingers from the keys.

use clipboard in terminal

You don’t have to use your mouse to copy and paste text in Terminal — it call all be done from the keyboard (Stux)

Why Do I Need to Use Clipboard in Terminal?

If you’re really asking this question, maybe you aren’t as much of a Terminal fanatic as you claim to be. Using pbcopy, you’re able to not only copy the contents of a file into the Clipboard, but also redirect the output of Terminal commands into it.

If you’ve ever tried to easily get the contents of a text file into the Clipboard, you know what a hassle it can be. You have to open the file in TextEdit, highlight the contents, and then use the Copy shortcut. If you want an easier method, pbcopy might just become your new best friend.

Copying to the Clipboard

There are two main uses I have for pbcopy. Those are copying text from a file, and redirecting output from a Terminal command. The first is the easiest:

pbcopy < file.txt

This will take all of the contents of file.txt and copy it to the Clipboard. You can then access it using the Finder’s Clipboard shortcuts or pbpaste.

Let’s look at a more complicated example. Let’s say I want to generate a text file of all of the processes running on my Mac. I could simply type ps aux and then try to highlight everything, but the output would likely span several screens. To more effectively use Clipboard in Terminal, I just do this:

ps aux | pbcopy

That command string will pipe the results of my ps command to the Clipboard. Again, I can use the Finder’s Clipboard shortcuts or the pbpaste command to access the text.

Pasting From the Clipboard in Terminal

Next up is pbpaste. This command retrieves the data from the Clipboard buffer, writing to the Terminal. If I just type pbpaste without any arguments, the command will output the Clipboard contents straight into my command line.

Let’s say I want the Clipboard contents to go into a file. For that, I just add an argument:

pbpaste > file.txt

That would insert the contents of the Clipboard into a file called file.txt.

That’s all cool and stuff, but what about filtering through the data? Take a look at this.

ps aux | pbcopy
pbpaste > file.txt

When I ran that set of commands just now, it generated a 430-line text file. What if I wanted to fiilter it to certain processes? For that, I use grep alongside either pbcopy or pbpaste. So, I do this:

ps aux | pbcopy
pbpaste | grep Chrome > file2.txt

Now I have a much more manageable text file; it’s only six lines instead of 430.

But Wait, There’s More

The pbcopy and pbpaste commands are insanely powerful, and I’m not even fully fluent in the intricacies of making the most of them. For example, there are multiple Clibpoards, or pasteboards, that the Terminal commands can work with. You can also specify what type of data to look for first in the pasteboard — plain text, rich text, or Encapsulated PostScript.

If you want to really brush up on how to use pbcopy and pbpaste, I recommend reading through man pbcopy in the Terminal. There are also several topics in the ADC Reference Library available to paid registered developers. These topics include:

  • Cocoa > Interapplication Communication > Copying and Pasting
  • Carbon > Interapplication Communication > Pasteboard Manager Programming Guide
  • Carbon > Interapplication Communication > Pasteboard Manager Reference.

As you can see, these are some pretty powerful commands. I’d say add them to my list of the five best Terminal commands you need to know about.

3 Comments Add a comment

  1. Scott B in DC

    You can also highlight sections of the terminal, including moving backward in the buffer, and press COMMAND-C to copy text from the screen.

    Conversely, if you do not understand HISTORY EXPANSION via the command line (and if you are a regular terminal user, you should learn this), you can copy and paste previous commands using COMMAND-C and COMMAND-V to paste. When you paste using COMMAND-V, it is treated like you typed the characters. You do not have to worry about cursor placement.

    One of the issues that someone needs to be addressed is how pbcopy and pbpaste interact with clipboard managers.

  2. Jeff Butts

    Yes, you can highlight sections of the Terminal. I mentioned that as being a hassle 🙂

    If you’ve ever tried to easily get the contents of a text file into the Clipboard, you know what a hassle it can be. You have to open the file in TextEdit, highlight the contents, and then use the Copy shortcut. If you want an easier method, pbcopy might just become your new best friend.

    As for history expansion and the clipboard, I’m struggling to find a case where that would come in useful. When I’m accessing my command history, it’s to re-execute those commands. I suppose, if I needed to change something, I could copy it to a text file, change it, and then paste it back…but it would be faster for me just to move the cursor where I need it and change the command directly.

    To answer your question about how pbcopy and pbpaste interact with clipboard managers, the answer is they don’t. They work solely with the built-in Clipboard, in its four varieties. What I don’t know yet, because I’m just scratching the surface of what the commands can do, is how those other three (besides the general pasteboard) can be accessed from the GUI.

  3. mlvezie

    Where it gets really fun is if you’re well-versed in UNIX tools. Let’s say you’re in $APP and need the data you copy to be changed in some way that $APP is clueless about, but you know you can do it easily using grep, sed, awk, et. al., you can do:

    pbpaste | grep | tr | sed | awk | etc | pbcopy

    So you’re in $APP, you copy all the data you want changed, switch to terminal run that, then paste it back in $APP. You can even make that a script or something.

Add a Comment

Log in to comment (TMO, Twitter, Facebook) or Register for a TMO Account