« August 22, 2004 | Main | August 26, 2004 »

August 24, 2004

Manipulating the clipboard in the Terminal: pbcopy and pbpaste

Mac OS X provides two utilities that make it possible to manipulate the clipboard in the Terminal: pbcopy and pbpaste.

pbcopy puts the data it receives via standard input into the clipboard.

echo "Hello, World!" | pbcopy
pbcopy < aFile

pbpaste does the opposite. It ouputs the clipboard to the standard output.

pbpaste
pbpaste > anotherFile

It is possible to select the preferred format for the clipboard data using the -Prefer option:

-Prefer rtf|ps|ascii

Both commands also accept the -help option:

Usage: pbcopy [-help]
Usage: pbpaste [-help] [-Prefer rtf|ps|ascii]
Do not meddle in the affairs of Coding Ninjas, for they are subtle and quick to anger.