Copy colorful terminal (emulator) text output and paste as HTML

copy/pastehtmlputtyterminal

I want to write notes (in HTML format) when learning Linux. When I need to note colorful terminal text output, I want the color of terminal text can be remained in my notes, e.g.:

Note 1: Test

The output of xx command is

<pre>
[root@webserver ~]# ll /
total 100
drwxrwxrwt.   7 root root  4096 8月  24 12:22 <span style='background-color:green; color:blue;'>tmp</span>
drwxr-xr-x.  14 root root  4096 7月  19 21:20 <span style='color:blue;'>usr</span>
</pre>

I can manually add HTML code wrapper around the plain text, but I hope there's an automatic way to achieve that.

Currently, I'm using putty on Windows to connect to remote CentOS linux server.

Best Answer

Use the script utility to capture the terminal output to a file (ansi color control characters included) and then convert it to html with ansi2html script. Eg:

inigo:tmp> script
Script started, file is typescript
inigo:tmp> ls
#
# lots of colour output
# 
inigo:tmp> exit
exit
Script done, file is typescript
inigo:tmp> cat typescript  | ansi2html.sh > typescript.html

If using PuTTY, "All session output" login option will also keep the ansi color codes.

As an alternative to ansi2html, you could use aha - Ansi HTML Adapter.

Related Question