Copy colorful terminal (emulator) text output and paste as HTML?
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.
Solution 1:
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.
Solution 2:
After seven years and nine months, putty still don't support 'Copy as HTML' feature.
I had already turned to Fedora linux as my primary operating system for work for about six years and nine months because of end-of-life of Windows XP.
Now, gnome-terminal v3.x had added 'Copy as HTML' feature for 3 years (git commit GNOME bug report), and it works as what I expected, so I will use gnome-terminal to do this job if I needed. See the screenshots below.
GNOME terminal 3.36.1.1 window
The HTML code copied from GNOME terminal
<pre><font color="#00D700">root</font> @ <span style="background-color:#005FAF"><font color="#00005F">tx300</font></span> <font color="#00FFFF">/tmp</font> <span style="background-color:#AF00AF"><font color="#FF0A0A">2020-05-29 16:34:00</font></span>
# ls /usr/bin/vi*
<font color="#00FFFF">/usr/bin/vi</font> <font color="#00D700">/usr/bin/virt-alignment-scan</font> <font color="#00D700">/usr/bin/virt-edit</font> <font color="#00D700">/usr/bin/virt-ls</font> <font color="#00D700">/usr/bin/virt-tail</font>
<font color="#00FFFF">/usr/bin/view</font> <font color="#00D700">/usr/bin/virt-builder</font> <font color="#00D700">/usr/bin/virt-filesystems</font> <font color="#00D700">/usr/bin/virt-make-fs</font> <font color="#00D700">/usr/bin/virt-tar-in</font>
<font color="#00D700">/usr/bin/viewres</font> <font color="#00D700">/usr/bin/virt-builder-repository</font> <font color="#00D700">/usr/bin/virt-format</font> <font color="#00D700">/usr/bin/virt-manager</font> <font color="#00D700">/usr/bin/virt-tar-out</font>
<font color="#00D700">/usr/bin/vim</font> <font color="#00D700">/usr/bin/virt-cat</font> <font color="#00D700">/usr/bin/virtfs-proxy-helper</font> <font color="#00D700">/usr/bin/virt-pki-validate</font> <font color="#00D700">/usr/bin/virt-win-reg</font>
<font color="#00FFFF">/usr/bin/vimdiff</font> <font color="#00D700">/usr/bin/virt-copy-in</font> <font color="#00D700">/usr/bin/virt-get-kernel</font> <font color="#00D700">/usr/bin/virt-qemu-run</font> <font color="#00D700">/usr/bin/virt-xml-validate</font>
<font color="#00D700">/usr/bin/vimdot</font> <font color="#00D700">/usr/bin/virt-copy-out</font> <font color="#00D700">/usr/bin/virt-host-validate</font> <font color="#00D700">/usr/bin/virt-rescue</font> <font color="#00D700">/usr/bin/vi.vi</font>
<font color="#00D700">/usr/bin/vimtutor</font> <font color="#00D700">/usr/bin/virt-customize</font> <font color="#00D700">/usr/bin/virt-index-validate</font> <font color="#00D700">/usr/bin/virt-resize</font>
<font color="#00FFFF">/usr/bin/vimx</font> <font color="#00D700">/usr/bin/virt-df</font> <font color="#00D700">/usr/bin/virt-inspector</font> <font color="#00D700">/usr/bin/virt-sparsify</font>
<font color="#00D700">/usr/bin/virsh</font> <font color="#00D700">/usr/bin/virt-diff</font> <font color="#00D700">/usr/bin/virt-log</font> <font color="#00D700">/usr/bin/virt-sysprep</font>
<font color="#00D700">root</font> @ <span style="background-color:#005FAF"><font color="#00005F">tx300</font></span> <font color="#00FFFF">/tmp</font> <span style="background-color:#FF87AF"><font color="#8A8A8A">2020-05-29 16:34:03</font></span>
#
</pre>
HTML render result (Firefox 76.0.1)