pbcopy and pbpaste with Terminal in MacOS

Print Friendly, PDF & Email

The MacOS shell is very powerful to let you solve all your problems without making use of graphical tools. However, I am pretty sure an awful lot of users are not aware of some wonderfully useful commands which are almost entirely unknown to the community at large. In this post, I will show you two of these forgotten commands are pbcopy and pbpaste. Let’s take a quick look at what they can do.

pbcopy and pbpaste with Terminal in MacOS
pbcopy and pbpaste with Terminal in MacOS

pbcopy

This command allows you to copy text from stdin into the clipboard1 buffer. Trivial example:

echo 'Hello World!' | pbcopy

“Hello World!” is now in your clipboard.

pbpaste

Pastes from your clipboard to stdout. Trivial example:

echo `pbpaste`

This will echo the contents of your clipboard. If you’re following along you’ll see “Hello World!”.

Some use cases

What can’t you do! Oh, you want examples? Well…

  • You could grab the output of a grep/awk/sed to paste into IM/IRC.
  • You could use a macro tool (like iKey, QS, etc) to create text modifying workflows that grab highlighted text, manipulate it, and replace it inline.
  • You could pull changelogs from svn into the clipboard when tagging for release so you could email them to coworkers.

Let me know what amazing things you come up with to enhance your own productivity!


  1. or pasteboard, hence the prefix “pb”

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code

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