Using ansi2HTML to create colored Terminal code as HTML
If you’re on macOS or Linux or have a Linux shell running on Windows, you can use ansi2HTML to output colored Terminal code into Microsoft Word.
Install ansi2HTML
Without access to yum, we need a package manager to allow us to install ansi2HTML. Unfortunately, the package is not on Homebrew, but we can get it from pip. If you don’t already have pip installed, grab it by running the command below in Terminal: Once you have pip installed, you can pull ansi2HTML down from pip with this command: Linux users with yum available can use the following commands to install:
Using ansi2HTML in Terminal
With ansi2HTML installed, we can now use it in Terminal. The general syntax for ansi2html is: First comes the command we want to print out with any colorization flags enabled. Then we pipe that output to ansi2html and use a caret to export it to a file with the .html extension. For example, to print my macOS system logs in color, I might run something like the following: Eagle-eyed readers will note that I’ve interspersed ccze as a secondary step to colorize my logs. You can grab that from Homebrew with brew install ccze. Open the resulting HTML file, and we will see the colorized output of our command.
Note that you need to be careful to explicitly colorize the code coming out of Terminal, as the pipe command ( | ) often strips colorization even if your environmental variables request otherwise.
Pasting Colored HTML into Word
Now that I’ve generated colored HTML, I need a way to put that into Word.
- From the “Insert” menu on the ribbon in Microsoft Word, select “Text from File” under the Object icon’s dropdown.
If you don’t see that icon, make sure you’ve maximized your Word window to reveal all the icons in the ribbon. 2. In the pop-up file browser, select the HTML file that ansi2HTML created. The colored text will be copied into Word.
Changing ansi2HTML’s color scheme
As you can see, some of the text is a little difficult to read. We can adjust the colors manually in two places. In this instance, the first would be the ccze command, which was responsible for applying the color initially. But more directly, we can edit the CSS in the HTML file that ansi2HTML created.
Open the HTML file in a text editor.
In the head of the HTML document we can see the auto-generated CSS.
ansi2HTML defines a number of classes to apply colors. We simply need to see which classes are associated with the colors we want to change, then pick a more suitable color. We can also run ansi2HTML in light color mode using the -l flag, like so: This produces much more legible output against a white background, though it’s not as attractive:
You could also put shorter code blocks in a textbox and set the background to a dark color, but that’s probably the least flexible option.
Conclusion
A similar process can be used to colorize a variety of code. If you can produce colorized standard output in Terminal, you can “print” that to ansi2HTML and drop it into Word.