If you use terminal on a daily basis I bet you played with some color settings at least once. Colorizing
ls output, shell prompt,
git logs - those are very common tasks. And it’s not surprising as color helps us parse information faster, pay attention to important parts and generally makes things prettier. Everybody loves pretty things, especially the ones you have to look at every day.
This post is an attempt to gather in one place and structure things I know about colors in terminal.
You’ve probably seen things like
\x1b[1;31m. These are ANSI escape codes used for defining a color. All ANSI escape sequences start with, well,
ESC. There’re several ways of encoding an
\033[31;4m are different ways to write the same sequence. Let’s look at the structure of this sequence.
\x1b[ is a Control Sequence Introducer that consists of hexadecimal ASCII
ESC character code and a
31;4 is a list of instructions separated by
;. Usually this list is formatted as follows:
31;4 means “no prefix, color - red, underline”.
<PREFIX> is used for 256 color mode. More on color modes later.
m indicates the end of control sequence so terminal would know not to interpret text after
m as a color code.
The following command should print “hello” in red underscore text:
\x1b[0m means “reset all attributes”.
Back in the old days terminals were different. Some of them could display only 16 colors and some of them went as far as 256. Now you probably work with a terminal emulator that runs on a machine that could display more than 16 million colors. But as terminal applications emulate older terminals, they usually support far less colors. For example a terminal app could be set up with 16 colors support or 256 colors support. The exact values of those colors depend on a terminal’s settings.
To list all available colors in 16-color mode run:
You should see something like this:
Or, in fact, something completely different! You can set up red to look like blue but I wouldn’t recommend such a deception as it probably mess up some color themes in
zsh or anything else that runs in a terminal.
As you can see from command’s output there’re several sets of color codes:
|Basic 8 colors||30..37|
|Basic "high contrast" colors||90..97|
And a set of text decoration indicators that should be placed right after the color code:
If color code is prefixed by
38;5 it is interpreted as one of 256 colors. E.g.
\e[38;5;91m will color following text purple, while
\e[91m will indicate bright red.
There’re several 256 color palettes out there. For example, a couple of the popular ones are Web safe colors and X11 colors. And though they’re both include 256 colors, they’re two different palettes!
To enable 256 colors support, you have to set up your terminal as
iTerm2 go to Preferences > Profiles > Terminal > Report Terminal Type and put
xterm-256color into the field). The set of colors you’ll get is xterm-256 pallete. In some places, like Mac OSX default color picker, this palette is called “emacs”, though it doesn’t have anything to do with Emacs.
To list all colors available in 256 color mode with their codes run
Bonus: To list all colors available in