ANSI Colors

03 Oct 2012


I love ANSI escape sequences. You can use them to add color to your terminal apps, which I think is all kinds of awesome. I like to throw them anywhere and everywhere, since they’re so simple and easily add a bit of readability to output or even input.

For example, many people’s first experience with ANSI colors is by using Vim. Vim has great support for syntax highlighting in many languages and is extensible to just about any language with syntax files. You can enable syntax highlighting in Vim by typing :set color in normal mode. You can also change the color scheme in Vim.

Another common first encounter with ANSI colors is in git. If you haven’t done this already, do git config --global --add color.ui true. This will enable color in all your git commands, which makes it a lot mroe readable.

The way these terminal apps implement these colors is actually rather simple. ANSI escape codes for colors look something like \033[Xm, where X is the specific color code you want to use, and \033 is the escape character. You can enter the excape character in Vim while in insert mode by pressing Ctrl-v, and then hitting the Esc key. It ends up looking like ^[ in Vim.

If you’re creating terminal apps in Node.js, then I have to recommend Nathan Rajlich’s ansi.js. It generates ANSI color escape codes for you using basic color names and RGB sequences, so you never have to worry about weird things like entering the escape character manually.

A while back, I wanted to learn a bit more about ANSI colors, so I created two barely useful tools that were fun to make, and do interesting things with ANSI color codes.

The first is hashcolors. This adds a few terminal apps to your PATH that give visual representations of various hash functions applied to files or strings. I used ansi.js, which made it very easy to build. When applied to the tarball of the version of Node.js I built this with, it looks something like this:


The second is md5flag. This adds a similar command, but instead of just outputting the colors representing a hash, it generates a “free speech flag” rendition. This is just a fun little toy.

More recently, I built a QR code generator that emits QR codes into the terminal using ANSI color codes. It’s built as a web service, so you can use curl, like this. I’ve also rebuilt it, in an attempt to re-learn C:

qransi in C

You could use this to make iOS or Android apps that tie in to terminal apps. Perhaps server status monitor apps?

And now, for some extra fun, try curl :)

2023 Update: is long gone. That said, curl-ing this site still gives you some cool ANSI art.