Copy colored output from Mac OS X terminal

Solution 1:

It appears that if you select text in the Terminal, then copy it, then paste it into an application that supports rich text (e.g. Mail, TextEdit, Word), then the text color will be preserved.

It appears that in Mail and TextEdit, the background color of the terminal is also pasted; in Word, it is not. I'm not sure how to explain this discrepancy.

Solution 2:

Amazing how posting a question boosts ones creativity.

I can open "Print" dialog and save to PDF and then copy colored output from the PDF.

Also iTerm2 terminal app has a shortcut CMDOPTC that copies with formatting.

Solution 3:

you can try to paste the colored output to the clipboard like this:

% command_which_produces_colors | pbcopy -prefer rtf

which puts some rtf (possibly colored and formated) stuff into the pasteboard of macos. you can then use any other that understands richtext to access the clipboard.

this is obviously not the same as "select it with the mouse" but maybe worth a shot.

Solution 4:

Based on @akira's answer, I came up with the following:

  1. Download the source package for ansifilter (can also be installed by brew install ansifilter)
  2. Follow the instructions under INSTALL to build and install ansifilter
  3. Run <command that produces colors> | ansifilter --rtf | pbcopy -Prefer rtf
    • You may need to pass arguments to <command that produces colors> to force it to produce colors even though its output is piped, e.g. ls --color=always instead of just ls
  4. You can then paste into an application that supports rich text (e.g. Mail, TextEdit, Word)

Solution 5:

OS X has several keyboard shortcuts (hotkeys) for copying the screen. These will save your screenshots as a jpg files on the desktop. Here's a list of them:

Cmd-Shift-3                             Take snapshot of the whole screen
Cmd-Shift-4                             Take snapshot of the selected area
Cmd-Shift-4-Spacebar                    Take picture of a window
Escape                                  Cancel
Hold Spacebar after Drawing the region  Move the selected area
Hold Option                             Resize selected area
Hold Shift                              Resize selected area horizontally 
                                        or vertically 

There's also a "Grab" application located in the Applications | Utilities folder that does the same job, plus has a few extra features such as delay.

Update

In Snow Leopard (10.6.8) the default screenshot format is png not jpg, although that can be changed to any one of many others -- including pdf -- using the free 3rd-party TinkerTool preference settings utility.