Copy colored output from Mac OS X terminal

37

13

I see a nicely colored output in my terminal.

The terminal has a "Export Text As" command but it saves a plain-text file without the colors. Same with copy&paste.

Do you know a way to save terminal output including colors?

Wojtek Kruszewski

Posted 2011-01-04T16:19:01.343

Reputation: 1 004

Terminal now has a list of options for the basic Cmd-c copy command under Edit -> Copy Special, where you can toggle between Plain Text and rich text options. – Joshua Goldberg – 2018-11-07T17:53:46.157

If this is important to you, please let Apple know via http://www.apple.com/feedback/macosx.html (Applications/Utilities feedback area) or https://developer.apple.com/bugreporter/ (requires registering for a free developer account).

– Chris Page – 2012-03-10T21:42:40.617

Answers

12

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.

Max Nanasy

Posted 2011-01-04T16:19:01.343

Reputation: 394

What version of OS X are you using? This doesn't work on 10.7.4. – Wildcard – 2016-03-18T00:45:48.680

1@Wildcard It works for me on 10.11.3. I don't remember which version I was using when I wrote this – Max Nanasy – 2016-03-18T00:48:43.717

1I had trouble using regular Copy to paste into Mail — the colors were lost — but it worked when I used Edit -> Copy Special -> Copy Without Background Color (which I preferred anyway). It even worked with option-drag to create a rectangular selection. (10.12.6) – Joshua Goldberg – 2017-09-06T19:58:33.417

32

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.

Wojtek Kruszewski

Posted 2011-01-04T16:19:01.343

Reputation: 1 004

+1 for the iTerm2 shortcut! Why is that not even in the meny??? – Per Lundberg – 2017-01-30T05:42:56.940

1That ... is definitely the most useful find since a long time!!! – haavee – 2017-11-03T12:56:28.000

If you ⌘ + ⌥ + C from iTerm2, open Keynote, and paste it, it will import it as table data. I haven't quite figured out how to format the data in a useful way, but there's potential there. – Kevin Suttle – 2018-09-14T14:24:34.613

1This solution is perfect for maintaining colored output when creating terminal examples in a Keynote presentation. Thx! – beausmith – 2011-09-19T01:25:10.430

1@Wojtek: You're welcome, even though it cost me a little reputation when you unaccepted my answer... ;-) – martineau – 2012-03-06T19:36:02.263

8

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.

akira

Posted 2011-01-04T16:19:01.343

Reputation: 52 754

1When I try this, if command_which_produces_colors produces ANSI codes, then pbcopy -Prefer rtf just copies the text including the ANSI codes (e.g. it copies [0;34mText for blue Text). It seems as though converting the ANSI codes to RTF may be required as an intermediate step. – Max Nanasy – 2015-01-08T02:13:04.837

Doesn't seme to work. First problem is that command_which_produces_colors notices it doesn't print to the console so it skips the colors. I can force some of them to print colors anyway, so following command outputs colors to the terminal: CLICOLOR=1 CLICOLOR_FORCE=1 ls -alh | tee But pbcopy copies no colors: CLICOLOR=1 CLICOLOR_FORCE=1 ls -alh | pbcopy -prefer rtf

+1 for pbcopy/pbpaste commands. It's great for copying long files to clipboard: "pbcopy < longfile.txt" – Wojtek Kruszewski – 2011-01-13T07:46:28.933

8

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

  1. Download the source package for 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)

Max Nanasy

Posted 2011-01-04T16:19:01.343

Reputation: 394

2

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.

martineau

Posted 2011-01-04T16:19:01.343

Reputation: 3 849

9a .jpg is kind of not as nice as "pure" text. – akira – 2011-01-04T18:23:01.393

3+1 for the lesser known shortcuts for selection area. – Daniel Beck – 2011-01-04T19:05:03.383

@akira: The OP wants colors -- something you can't do in a plain-text format unless you count embedded ANSI escape sequences -- and those would need something to interpret them like a terminal emulator, but then you're back to the original problem.

– martineau – 2011-01-04T21:11:16.170

2@martineau: depending on the capabilities of the clipboard colors are saved by markup (eg, richtext or html) when you copy from a browser or from word or from xyz. and, depending on the application that receives such marked up snippets they can show the colors AND the text quite ok. the terminal receives ascii escape sequences, correct, but just because it receives ascii escape sequences does not mean it has to a) store them (eg, think html-terminal) b) it can enhance the selected text a lot of ways (you can put different mime-types at a good clipboard at once). – akira – 2011-01-04T21:48:10.397

@akira: IF the application allows you to select some or all off what appears in its window(s), and IF it also supports Cmd-C (copy), allowing you to copy that to the clipboard, and IF you have some other application running that understands the format of what was placed on the clipboard, then yes, after a Cmd-V (paste) you might end up with something that had the text in it in a form you could edit and/or restyle. It's not clear at all that's what the OP wanted. The OS screengrab keys listed work with any application (or potentially multiple ones at the same time). – martineau – 2011-01-06T03:00:46.510

take a short look at http://manpagez.com/man/1/pbpaste/osx-10.4.php and http://www.devx.com/opensource/Article/37233/0/page/3 ... and yes, obviously makeing pixels is 'easier' at creation time, its just that ocr doesnt work that well if you want to extract that text back, thats all i was saying.

– akira – 2011-01-06T06:32:57.510

Yes, that's a viable alternative (less key strokes) to printing to PDFs when you:

  1. need only small portion of console output
  2. you don't need to be able to select and copy text from this saved output
  3. < – Wojtek Kruszewski – 2011-01-13T07:55:23.823

3

@akira A JPG is a really poor choice of image format for something like a screenshot. Large areas of single color and sharp single pixel lines etc. all over it. Fortunately, Apple are not that silly and the file it saves is a PNG. http://lbrandy.com/blog/2008/10/my-first-and-last-webcomic/

– rjmunro – 2012-03-01T11:09:59.783

@rjmunro: yeah, especially if you make screenshots from applications like photoshop or a browser showing something like http://flic.kr/p/avFtqb ... so, thank you for your wisdom on this important issue.

– akira – 2012-03-01T12:17:39.923

0

The way I do this is via the excellent Textmate 2 app.

  1. Copy from R graphical window
  2. Paste into an empty textmate document
  3. Change document type to R Console or R as preferred
  4. Use the copy-with-style tmbundle (shift-cmd-c)
  5. Paste into mail app compose window

I have to say it would nice if shift-cmd-c worked directly in the R console window.

Michael Neale

Posted 2011-01-04T16:19:01.343

Reputation: 1