Leveraging the macOS clipboard on the command line with ‘pbcopy’ and ‘pbpaste’

Bruno Berstel-Da Silva
Decision Optimization Center
3 min readJan 17, 2024
Collage — Photo by Gareth David on Unsplash

Do you know these killer macOS Terminal commands: pbcopy and pbpaste? Here, ‘pb’ stands for paste bin, a.k.a. the clipboard, that is, the memory buffer to which you copy things with ⌘C and from which you paste them with ⌘V. These commands allow you to combine the comfort of copy-paste with the power of the command line.

A few examples

These are just some use cases that I happened to cross lately. You will probably find yours by chance. (Usually after struggling with ⌘C/⌘V.)

Saving an API key. For security, an API key is not stored by the web application that issues it. As a consequence, it is shown to you only once and you have to copy it (many applications provide a Copy to clipboard button) and save it somewhere. In my recent usage, I wanted to save the API key into a text file. I did that with

pbpaste > api-key.txt
API Key creation dialog in DB Gene

Pasting to Excel. Another task I wanted to achieve was to copy the output of a command line to an Excel spreadsheet. For the sake of this example, imagine that you want to measure the weight of the sub-folders in your Documents folder, and copy that to Excel. Here is your command line:

du -sh ~/Documents/* | pbcopy

That line stores the output of the du command into your clipboard. All you have to do then is to select a cell in your spreadsheet and type ⌘V.

Deduplicating lines. Before I learnt about the Excel feature to deduplicate values, I would typically copy the cells I want to deduplicate into the clipboard, run the following command line, then paste the clipboard back to Excel.

pbpaste | sort -u | pbcopy

Note that, as a side effect, the command line above sorts the values alphabetically. I’ll discuss this and alternatives in another post.

Why use commands rather than ⌘C/⌘V?

The examples above can in most instances be performed using the keyboard shortcuts for copy/paste (or the menu entries for the mouse-inclined). However, there are two main reasons why I would want to use the pbcopy and pbpaste commands.

One reason is that it makes command lines self-contained. What I mean is that, by reading the command line, you can clearly see when the transfer between the clipboard and the command line happens. Granted, this argument is a matter of style, and may be discarded for one-shot commands. It becomes more relevant when you need to repeat the command several times in your workflow.

The other reason is that these commands preserve the exact content of the clipboard. This is especially important when such distinctions as between tabs and spaces matter—typically when copy-pasting in Excel. If you use your mouse and keyboard to select and copy the output of the du command above, you will get a variable number of space characters between the first and second columns. With pbcopy, you will get the exact output of du, that is, a tab character.

In the same vein, I noticed that when the content of the clipboard is too long, pasting it into the Terminal does not work well. This is typically a case where pbpaste is better suited.

That’s it for this post. Have fun with pbcopy and pbpaste, and share your smartest uses in comments!

Bruno is CTO at DecisionBrain. DecisionBrain delivers custom decision support software that is used to solve the world’s hardest planning, scheduling & logistics optimization problems. With over 400 person-years of experience in operations research and mathematical optimization, DecisionBrain helps where packaged applications fall short.

--

--