Making Text Files Colorful – With a Hex Editor

Hello, friends. In this tutorial I will be sharing a neat hack that you can use to add colors and other styles to a plain text file. This means that when you view the file with cat, the text will be color-coded. We accomplish this using ANSI escape sequences, a text editor, and a hex editor.

ASCII has a number of control codes, which are non-printable characters that you can’t type on a keyboard. Therefore if you want to include ASCII control characters in a file, you must enter the hex codes directly in a hex editor. One of these control characters is the Escape or ESC character (technically ESC is an acronym that stands for something else, but we might as well just call it Escape). ESC is used to denote sequences of control characters called ANSI escape sequences. In ye olden days of Unix, these control characters and escape sequences were literally commands for a VT100 or other DEC terminal. Unlike regular text commands, they were processed locally by the terminal rather than being sent to the mainframe or minicomputer at the other end.

There are ANSI escape sequences for controlling the text color as well as the background color of text in the terminal. There are also sequences for bold, italic, underlined, and blinking text, though not all terminal emulators will support all of these. You can find a complete table of ANSI escape sequences here. (Might want to open it in a new tab so you can refer to it while reading this tutorial.)

I’m going to take a sample text file and add ANSI colors to it. The file I’m using is a sample user database file copied from /etc/passwd (be careful not to use your actual passwd file for this). At the moment this file has no ANSI sequences or styles, so when we run cat on the file, we just see boring monochrome text.

cat of a text file with no ANSI colors added
cat of a text file with no ANSI colors added

Now we’re going to add the ANSI escape sequences. First we open the file in a text editor like Vim. We can add the entire escape sequences in Vim, with a placeholder character for ESC, which we will replace later in a hex editor (hex editors edit files in-place, so we won’t be able to insert the ESC character there). To avoid confusion later on, make sure your placeholder is a character that’s not used elsewhere in the file.

Inserting ANSI escape sequences into a text file with Vim
Inserting ANSI escape sequences into a text file with Vim

To make things easier, if you have a lot of ANSI sequences to add, you can use the find/replace functionality of your text editor. I used the following editing commands in Vim (shown here in script form):

 1 " Add ANSI escape sequencecs
 2 " with placeholders
 4 %s/:/@\[0m:/g
 5 %s/^/@[31m/
 6 %s/:/&@\[32m/
 7 %s/@\[32mx@\[0m:/&@\[33m/
 8 %s/@\[33m[0-9]*@\[0m:/&@\[34m/
 9 %s/@\[34m[0-9]*@\[0m:/&@\[35m/
10 %s/@\[35m[^:]*:/&@\[36m/
11 %s/[^:]*$/@\[31m&/

Once you are done inserting the ANSI escape sequences, save your work and close the text editor, then open the file in a hex editor. I’m using hexedit for this, since this hex editor is easily accessible through basically any Linux package manager. Go through the file in the hex editor and replace all instances of the placeholder character with 1B (the hex code for the ESC character).

Adding ESC characters to a text file with a hex editor
Adding ESC characters to a text file with a hex editor

Save your work (F2 in hexedit) and then close (F10 in hexedit). Then at the command line type cat filename. We get an output like this:

Output of text file with ANSI escape sequences
Output of text file with ANSI escape sequences

As you can see, we’ve now added pretty ANSI colors to the file and we don’t need some special program to display them. This is because the cat program processes ASCII control codes like any other character – it reads them from the file and echoes them. This allows you to do some other tricks as well. For example if you put a BELL character in a text file, your terminal will literally beep when you cat the file.

Incidentally, this is also how ANSI art is made. ANSI escape sequences are inserted into a text file to control foreground and background colors, and then when the file is displayed to the terminal, it displays with all the ANSI colors that are coded into it, making a pretty picture. You can use the hack I showed you here to make your own ANSI art. Good luck and have fun!


4 thoughts on “Making Text Files Colorful – With a Hex Editor

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s