Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

There are tools providing coloured output:

dwdiff -c File1 File2 # word level diff
grep --color=always # we all know this guy

The question is: How to convert their colored output of arbitrary program into coloured html file?

Other output formats might be suitable as well (LaTeX would be great). I think html is good starting point, as it's easy to convert it to other formats.

(For curious how to keep terminal colour codes, please follow answer: http://unix.stackexchange.com/a/10832/9689 ... | unbuffer command_with_colours arg1 arg2 | ... - tool unbuffer is part of expect )

share|improve this question
up vote 10 down vote accepted

The answer to this question is probably what you want.

It links to these tools, which do the conversion you're looking for:

share|improve this answer
The 'aha' link is broken. – Wildcard Mar 18 at 1:13

Or just a shell script


More details of this script http://pablomarin-garcia.blogspot.com/2011/04/converting-ansi-to-html-how-to-convert.html

share|improve this answer
Welcome to Unix & Linux Stack Exchange! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – slm Aug 29 '13 at 11:43

You can use vim. This is part of a script I use to convert diff output to HTML.

vim -n \
    -c ':%s%^+  %+++    ' \
    -c ':%s%^-  %---    ' \
    -c ':%s%^   %       ' \
    -c ':set nu' \
    -c ':let html_use_css=1' \
    -c ':so $VIMRUNTIME/syntax/2html.vim' \
    -c ':wq' -c ':qa!' $input > /dev/null 2> /dev/null

After this, I run sed to change the CSS and the title to be exactly what I want them to be.

*Edit: I should have mentioned that to get color, you need to have syntax highlighting turned on. I do that in my .vimrc, but if you wanted to add it here it would just be another line like

-c ':syntax on' \
share|improve this answer
Ok. So how would you like to convert output of arbitrary program, let's say : grep ? – Grzegorz Wierzowiecki Aug 8 '12 at 7:37
Ok, I didn't understand at first. If you want to convert ANSI color codes, then the answer here is probably what you want. If you just need a quick hack, then creating a sed script with a bunch of lines that replace ANSI codes with HTML would work. Something like 's,^[\[31m,<span style='color:red'>,g s,^[\[m,</span>,g' would be quick and dirty, but possibly sufficient. – user17591 Aug 8 '12 at 15:26
Could you @user17591 put this link as one more answer? I'd love to select it as answer to my question, and higher your reputation. – Grzegorz Wierzowiecki Aug 12 '12 at 19:31
Ok. I added a direct link to the Perl module so that any subsequent user will be able to find it with one less level of indirection, but also a link to the original question/answer for attribution. – user17591 Aug 13 '12 at 14:21
@user17591, nice solution! (+1) I just end-up adding a 2 year old vimshell followup variant of your solution! – JJoao Jan 26 '15 at 23:19

You can try vim with AnsiEsc.vim plugin to view ANSI colors through escape codes, then redirect standard output to vim -, activate :syntax on and convert the file to html by vim command: :TOhtml. The generated html file should have coloured output.

To convert source code non-interactively into html, try the following command:

vim -E -s -c "let g:html_no_progress=1" -c "syntax on" -c "set ft=c" -c "runtime syntax/2html.vim" -cwqa myfile.c

Or use - instead of the myfile.c, to convert code from standard input.

share|improve this answer

Update: I've added a perl method, using module HTML::FromANSI from CPAN .. To install it, just extract it to a directory, and run, as root: perl -MCPAN -e 'install HTML::FromANSI' from that directory. The particular feature to suit your request is a script called ansi2html. Interestingly enough, it shows the same loss-of-color after the overlaid k in the filenames, as does the elisp-shell script... Here is an example usage:

ls -l --color=always /bin/*k* | 
 grep --color=always "k\|x\|2010" |
  ansi2html  >/tmp/example.html
firefox /tmp/example.html

Here is the html output. as seen in Firefox:

enter image description here

Here is a method using an emacs elisp-shell script... for the example, called htmlize ...Emacs does not need to be running.

I originally tested it on a black background, but I noticed that for some reason, a white background doesn't play well with one of the introduced Escape Codes,\e[K, which seems to be ERASE_LINE (Erase the current line of terminal output). I've added a line to remove this Escape Code. It now works for a white background.

Here is an example of color highlighted output from ls being piped to grep for further color highlighting.

ls -l --color=always /bin/*k* | 
 grep --color=always "k\|x\|2010" >/tmp/example
htmlize /tmp/example
firefox /tmp/example.html

this is the elsip-shell script.

":"; exec /usr/bin/emacs -Q --script "$0" -- "$@" # -*-emacs-lisp-*-
(require 'ansi-color) (require 'htmlize)
(find-file (setq path-in (cadr argv)))
(ansi-color-apply-on-region (point-min) (point-max))
(switch-to-buffer (buffer-name (htmlize-buffer)))
(write-file (concat path-in ".html"))

Here is a sample of the html output, in Firefox... If I get time, I'll look further into the overlaying ANSI codes issue. where the red ks overlap the green filenames, but that is only there because of a hurriedly chosen test regex for grep... (maybe that is something \e[K influences...

enter image description here

share|improve this answer
Sounds nice. It would be great if there would be smaller tool for this task (emacs takes ~100MB). – Grzegorz Wierzowiecki Aug 8 '12 at 19:11

For those who have a command that is written on the assumption that piped output will never have color (e.g. some testing frameworks) you may find it useful to use the script utility to first save the ansi output. This can then be pushed to aha or the other utilities mentioned.

share|improve this answer

This is just a followup of @user17591 solution -- vim scripting:

#!/usr/bin/vim -ns
:%s%^+  %+++
:%s%^-  %---
:%s%^   %
:set nu
:let html_use_css=1
:so $VIMRUNTIME/syntax/2html.vim

chmod it and

Usage: htmlvim file (to produce file.html)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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