Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use the clipboard in the terminal UNIX
Over on the MacNN forums, "kvm_mkdb" posted a couple of useful commands regarding the Mac OS clipboard. If you're in the terminal, and want the output of a command on the clipboard, you can easily get it. For example, to dump a detailed directory list to the clipboard, just type:
ls -al | pbcopy #
You can then paste the contents of the clipboard using pbpaste:
pbpaste > somefile #
This would send the clipboard contents into "somefile". Of course, that's not a great example, as you could have just sent the directory list to the file in the first place (ls -al > somefile). However, it's more useful if you want to paste into a GUI-based application such as Word or BBedit. No more mouse selection required; simply use pbcopy, switch to the GUI app, and hit command-V.

I can't find a 'man' page or help file for either of these commands - anyone know if there are more options available?
    •    
  • Currently 3.40 / 5
  You rated: 1 / 5 (5 votes cast)
 
[14,463 views]  

Use the clipboard in the terminal | 6 comments | Create New Account
Click here to return to the 'Use the clipboard in the terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Options
Authored by: blb on Jun 27, '01 11:23:26PM

Both commands have a -help option, but that's all pbcopy seems to do. pbpaste has:

-Prefer rtf|ps|ascii



[ Reply to This | # ]
Options
Authored by: mnaj on Jun 28, '01 06:10:05PM

There are ancient commands from that let you copy and paste from the pasteboard buffer (when the prefix 'pb').
They come in real handy when doing work in Terminal.app. You can take any text and manipulate it with normal
unix commands, such as
pbpaste | grep "specific string" | sed -e 's/specific/certain/g' | mail username@domain.com.
This means you can take pbpaste between and Cocoa app and the unix command line. You can also load the
buffer thus
pbcopy <filename

Here're the manpages from when they were just copy-n-paste:

COPY(1) UNIX Programmer's Manual COPY(1)

NAME
copy, paste - provide copying and pasting from command line

SYNOPSIS
/usr/bin/copy

/usr/bin/paste [ -Prefer {ascii | rtf | ps} ]

DESCRIPTION
copy takes the standard input and places it in the
NEXTSTEP(tm) pasteboard (for more information on the paste-
board and pasteboard data types see the NeXT Developer's
Library, accessible through the NeXT Developer target of the
Digital Librarian). The input is placed in the pasteboard
as ASCII data unless it begins with the Adobe Systems Encap-
sulated PostScript file header or the Microsoft Rich Text
Format file header, in which case it is placed in the paste-
board as one of those data types.

paste removes the data from the pasteboard and writes it to
the standard output. It normally looks first for ASCII data
in the pasteboard and writes that to the standard output; if
no ASCII data is in the pasteboard it looks for Encapsulated
PostScript; if no EPS if present it looks for Rich Text. If
none of those types is present in the pasteboard, paste pro-
duces no output.

OPTIONS
-Prefer {ascii | rtf | ps}
tells paste what type of data to look for in the paste-
board first. As stated above, paste normally looks
first for ASCII data; however, by specifying -Prefer ps
you can tell paste to look first for Encapsulated
PostScript. If you specify -Prefer rtf, paste looks
first for Rich Text format. In any case, paste looks
for the other formats if the preferred one is not
found.

BUGS
There is no way to tell paste to get only a specified data
type.

$ man paste

COPY(1) UNIX Programmer's Manual COPY(1)

NAME
copy, paste - provide copying and pasting from command line

SYNOPSIS
/usr/bin/copy

/usr/bin/paste [ -Prefer {ascii | rtf | ps} ]

DESCRIPTION
copy takes the standard input and places it in the
NEXTSTEP(tm) pasteboard (for more information on the paste-
board and pasteboard data types see the NeXT Developer's
Library, accessible through the NeXT Developer target of the
Digital Librarian). The input is placed in the pasteboard
as ASCII data unless it begins with the Adobe Systems Encap-
sulated PostScript file header or the Microsoft Rich Text
Format file header, in which case it is placed in the paste-
board as one of those data types.

paste removes the data from the pasteboard and writes it to
the standard output. It normally looks first for ASCII data
in the pasteboard and writes that to the standard output; if
no ASCII data is in the pasteboard it looks for Encapsulated
PostScript; if no EPS if present it looks for Rich Text. If
none of those types is present in the pasteboard, paste pro-
duces no output.

OPTIONS
-Prefer {ascii | rtf | ps}
tells paste what type of data to look for in the paste-
board first. As stated above, paste normally looks
first for ASCII data; however, by specifying -Prefer ps
you can tell paste to look first for Encapsulated
PostScript. If you specify -Prefer rtf, paste looks
first for Rich Text format. In any case, paste looks
for the other formats if the preferred one is not
found.

BUGS
There is no way to tell paste to get only a specified data
type.



[ Reply to This | # ]
You can type anything
Authored by: donarb on Jun 28, '01 02:35:45PM

pbcopy can also accept text from the command line. If you type pbcopy, you then can type anything you want on the next line. Keep typing and hitting return. When you're done, hit ctrl-D. Now everything you typed is in the clipboard.

Don



[ Reply to This | # ]
Use the clipboard in the terminal
Authored by: ptwithy on Nov 26, '03 02:03:33PM

These commands are documented in Panther.



[ Reply to This | # ]
Use the clipboard in the terminal
Authored by: xtchall on Apr 05, '04 09:43:21PM

I'd hope to make use of this tip, but was disappointed to find output as follows:

diskutil(8) BSD System Manager's Manual diskutil(8)

NNAAMMEE
ddiisskkuuttiill - Modify, verify and repair local disks.

SSYYNNOOPPSSIISS
ddiisskkuuttiill _v_e_r_b [options]

DDEESSCCRRIIPPTTIIOONN
ddiisskkuuttiill uses the Disk Management framework to manipulate local disks.

VVEERRBBSS
Each verb is listed with its description and individual arguments.

lliisstt [_d_e_v_i_c_e]
List the partitions of a disk or all disks. If no device is
listed, than all partitions on all disks will be displayed.

iinnffoo | iinnffoorrmmaattiioonn _d_e_v_i_c_e
Get information on a disk or volume.

uunnmmoouunntt [force] _d_e_v_i_c_e
Unmount a single volume. Force will force unmount the vol-
ume.

Any ideas as to how to clean up the output?



[ Reply to This | # ]
Use the clipboard in the terminal
Authored by: vramin on Apr 06, '04 03:18:28AM

man pages are contain a lot of information for formatting so the command

man man | pbcopy

will result in a lot of garbage on the clipboard. Commands that have plain text results work fine.

Yo get man mages to the clipboard you could use something like:

man TheManpageYouWant | groff -t -e -mandoc -Tascii | col -bx | pbcopy



[ Reply to This | # ]