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 ?
Thanks.
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 nameansi2html
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.
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.
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