As everybody, i have a few colorized output in my bash. I want to keep the colorized syntax when copying an extract to a HTML editor for example. Is there a way to handle this ?



If you are talking about colorized output of some command line utilities like ls, grep, cmake, git diff, etc, please note, that it doesn’t have any relations to GNU Bash.

Anyway, there is several tools for converting output colorized with ANSI escape sequences to HTML:

  • aha from package of the same name
  • ansi2html from HTML::FromANSI Perl module (not packaged in Ubuntu repo, can be installed from CPAN)
  • ansi2html.sh by pixelb (not packaged, download from git)

Usage of all of them is rather simple: they read terminal escape sequences from stdin and put HTML to stdout. But note: most of utils don’t colorize output if it is not going to terminal. You have to order them to produce colorized output explicitly. E. g.:

$ ls --color=always | aha | xsel -i -b

xsel -i -b here will copy HTML to clipboard.

| improve this answer | |
  • This (aha) worked beautifully; thank you very much! – ShreevatsaR Jan 8 '15 at 18:57
  • 2
    aha - brilliant! This is almost perfect for me. To be able to copy formatted text, I adapted to route to tmp, open in browser, then just copy/paste as for other sites: git -c color.ui=always diff | aha > /tmp/diff.html && firefox /tmp/diff.html ...and yes I know the OP wanted the html, but many of us will stumble here wanting the formatting generally. – sage Oct 13 '15 at 16:28

You need to use javascript libraries for that, you cannot directly copy/paste and keep the colours as they are. Some of the popular libraries for this are:

Source: Similar question on SO

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.