How to Copy Text to the Clipboard from Mac Terminal

Copy Text to the Clipboard in Mac Terminal

Tired of going through hoops just to copy more than a screenful of text from the command line to another macOS app (say, a browser or word processor)? Find out here how to copy text from Terminal to the general Mac clipboard for pasting.

First, Corny Sound Effects

Corn is as much an auditory as it is a visual delight (not to mention gustatory sensation).

It plops during movie night preparations, of course, cracks as it grows and clicks underground with its roots.

Indeed, one can hypothesize that infant corn shoots communicate through sound and soil. Fresh growth happens towards a neighboring source of clicking.

Now, want your Terminal command line tools and visual macOS to connect as well? You need not even… click, just type:

How to Copy Text to the Clipboard from Mac Terminal

Using “pbcopy”

Time needed: 1 minute

To copy text using the Terminal prompt on a Mac:

  1. Direct the text you want to copy to the Mac clipboard to standard Terminal output.

    Standard output: Standard output is what you can see as the result of a program running in Terminal—except when that output is direct to a file (using >) or handed to another program to process (using |)
    Example: To print all HTTP headers of ladedu.com that contain “cache” to standard output, use
    curl --head --silent https://ladedu.com | grep -i cache.
    Superfluous cat: cat will print any file’s contents to standard output, but it usually is not necessary; see below.

  2. Now pipe standard input to the command pbcopy.

    Pipe: The pipe character (|) repurposes (standard) output as the (standard) input of the next command in line; pbcopy accepts standard input as the contents to be copied to the general clipboard.
    Type this: The command to pipe to pbocpy is typically  | pbcopy at the very end of the command line.
    Example: To copy caching headers for ladedu.com to the general clipboard (to be pasted anywhere in macOS using Command V), use
    curl --head --silent https://ladedu.com | grep -i cache | pbcopy.
    Fastes way to copy file contents: To copy a file’s contents (here the file example.txt) to clipboard, use
    pbcopy < example.txt
    (this redirects the file’s contents to pbcopy; it is an alternative to the unnecessarily complex cat example.txt | pbcopy).

  3. Press Enter.

    It’s copying: The text is now copied to the clipboard and ready for pasting anywhere in macOS using Edit | Paste in the menu or the Mac keyboard shortcut Command V.
    Use pbcopy to copy from the Mac Terminal command line to the macOS clipboard

The other way around: How to Paste from the Clipboard in Mac Terminal

Using macOS Clipboard Copying in Terminal

You can also, albeit with much less flexibility, copy using standard macOS copying:

To copy text visible in a Terminal window or tab to the clipboard (including formatting):

  1. Highlight the text you want to copy using the mouse.
  2. Press Command C.
    Menu alternative: You can also select Edit | Copy from the menu, of course.
    Copying plain text: To copy only the plain text, select Edit | Copy Special | Copy Plain Text from the menu or press Command Option Shift C.

How to Copy Text to the Clipboard from Mac Terminal: FAQ

Can I copy rich-text formatting using pbcopy?

Yes, to a limited extent.

Pbcopy will copy formatted text if (and only if) you feed it an RTF (*.RTF) file with rich-text formatting. All other documents and input (except PostScript files (*.EPS) will be copied as plain text to the clipboard.

Can I copy from within Terminal applications?

Yes, if the application lets you pipe contents to a shell command, you can use pbcopy as above to copy to the Mac clipboard. You can, for instance, use pbcopy to copy lines of text from Vim.

You can also, of course, highlight visible text and copy directly. Note that this will copy any parts of the interface (such as line numbers) that might also—and necessarily—be highlighted.

(How to copy text to clipboard from Mac Terminal tested with macOS Sonoma 14.3 and Ventura 13.4; updated March 2024)

Home » Mac Tips and Resources » How to Copy Text to the Clipboard from Mac Terminal