If you want to copy colored code from Terminal to Word with the correct colorization, simple copy and paste won’t work. You’ll need to use a tool to generate colorized HTML, then import the HTML into Word. We can use ansi2HTML to convert our Terminal code to HTML, then drop it into our Microsoft Word document.
This same trick will also work on Linux systems, as well as Windows systems with a bash shell available. Now that bash ships as part of Windows 10, that should cover virtually every computer out there.
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
Before we can start, we need a package manager to allow us to install ansi2HTML.
Many Mac users are familiar with Homebrew or Macports as their package manager of choice. Unfortunately, the package is not on Homebrew or Macports. Fortunately, we can get it from pip, the Python package manager.
macOS does ship with Python pre-installed, but that doesn’t include pip natively. It’s ultra-easy to install, however. Just run the command below in a Terminal window to install pip on your Mac:
sudo easy_install pip
Enter your root password, and pip will self-install in the proper directory. Once you have pip installed, you can pull ansi2HTML down with this command:
pip install ansi2html
(Linux users with yum
available can do an end run around all of this. Just use sudo yum install python-ansi2html
to install.)
Using ansi2HTML in Terminal
With ansi2HTML installed, we can now use it in Terminal.
The general syntax for ansi2html is like so:
ls -G | ansi2html > directories.html
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.
When running ansi2html, be careful to explicitly colorize the leading command. The pipe command ( |
) may strip colorization even if your environmental variables request otherwise, so you need to demand colors in the actual command.
In our example above, the -G
flag on ls
will add colors. That only works on macOS: Linux users will want ls --colors=always
instead.
Let’s try something more complex. To print my macOS system logs in color, I might run something like this:
sudo tail /var/log/system.log | ccze -A | ansi2html > logs.html
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:
Experienced systems administrators and pro Terminal users may know that ccze can itself output colored HTML, which can then be pasted into Word. However, ccze applies its own colorization rules rather than copying Terminal’s output, and it only runs on log files. ansi2HTML is the more flexible of the two programs, and directly copies Terminal’s standard output.
Pasting Colored HTML into Word
Now that I’ve generated colored HTML, I need a way to put that into Word.
1. From the “Insert” menu on the ribbon in Microsoft Word, select “Text from File” under the Object icon’s drop-down.
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.
1. Open the HTML file in a text editor.
2. In the head of the HTML document, we can see the auto-generated CSS.
3. 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:
ansi2html -l > logs.html
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 Text Box 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.