Ubuntu – Bash : colored copy / paste


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 ?


Best Answer

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.

Related Question