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.

Leave a Reply

Please Login to comment
3 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
3 Comment authors
mlvezieJeff ButtsScott B in DC Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

newest oldest most voted
Notify of

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.

Scott B in DC
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.