Color Code Cheat Sheet

January 3, 2024 ยท View on GitHub

Introduction

The following serves as a handy reference for creating ANSI escape sequences that can be used to configure eza, ls, or anything else that uses them to generate colorful text output in the terminal.

ANSI Escape Sequences

For color and text effects, Select Graphic Rendition (SGR) control sequences are used. The format for such sequences is:

\e + [ + n[;n] + m

ComponentDescription
\eis a literal 'escape' (may also be \x1b or \033)
n[;n]zero or more numbers from the tables below, separated by ";"
mis the code for the SGR function

A good way to think of such a sequence is as a function call where \e[ indicates you are calling a function, n[;n] represents the argument(s), and m is the function being called. 1^{1}

1^{1} Everything you never wanted to know about ANSI escape codes

Example

\e[01;04;33mHELLO WORLD\e[0m

The example above will result in the words 'HELLO WORLD' printed in bold (01), underlined (04), yellow (33) text. The sequence \e[0m after the text resets all effects and colors to the system default, which is typically no color and no effects.

Note that the leading 0 is not required, so \e[1;4;33m will yield the same results. In fact, \e[00m, \e[0m and \e[m are equivalent expressions for the global reset sequence.

Foreground/Background Colors and Resets

COLORNORMAL FGNORMAL BGBRIGHT FGBRIGHT BGRESETS
Black30409010000 Reset Everything
Red31419110139 Reset Foreground
Green32429210249 Reset background
Yellow334393103
Blue344494104
Magenta354595105
Cyan364696106
White374797107

Effects and Resets 2^{2}

CODEEFFECTRESET CODERESET NOTES
--All00Everything (colors and effects)
01Bold22
02Dim/Faint22Same as reset bold
03Italic23
04Underline24
05Blink25
07Inverse27
08Hidden28
09Strikethrough29
21Double Underline24Same as reset underline
53Overline55

2^{2} Not all color, effect, and/or reset codes are supported by all terminal emulators

256 Colors

To use colors from the 256 color palette available in most modern terminal emulators you must use the following control sequences:

TargetCodeNote
Foreground38;5;nnnnnn = three digit color number
Background48;5;nnn

Example: \e[38;5;197m results in very bright pink text.

RGB Colors

To use colors from the RGB color palette available in terminal emulators that support the 'true color,' or 16 million color palette, you must use the following control sequences:

TargetCodeNote
Foreground38;2;r;g;br/g/b = component numbers in the range 0-255
Background48;2;r;g;b

Example: \e[48;2;255;0;0m results in red text. 3^{3}

3^{3} Effect codes may also be applied to colors from the 256-color and RGB palettes.

A Special Note on Bold Text

Some (most?) terminal emulators use the bold effect (01) to enable bright, or 'intense,' colors. Often, this behavior will result in text that is both bold AND bright. For example: 01;34 (bold blue) would render the same as 01;94 (bold, bright blue).

This behavior is especially notable when trying to use bold black (01;30) as it will be rendered as bold, bright black (a.k.a. dark gray).

This 'functionality' is a holdover from a time when terminals only had eight colors and the bold flag was used to render a brighter variant of those eight colors. 4^{4} This behavior reduces your overall color options as you will not be able to render both bold normal text and bold bright text as distinct colors. Fortunately, you may be able to disable/modify such behavior in your terminal emulator settings. 5^{5}

For example, Windows Terminal has a setting for 'Intense text style' that can be set to 'bold', 'bright', or 'bold AND bright'. Setting it to 'bold' results in the expected rendering behavior. Similarly, Xterm, can be made to properly render bold variations by placing XTerm*boldColors: false in your .Xdefaults file. This setting may also work for other emulators, such as rxvt-unicode, that use the boldColors setting.

4^{4} 3-bit and 4-bit SGR
5^{5} If your color scheme uses the same color for black and the background, any bold black text will still be rendered the same color as normal black text, and it will therefore be invisible.

Other Notes

  1. An alternative to the bold black problem above is to use a gray from the 256 color palette (38;5;244), or you can use dim white (02;37).
  2. Bright black is dark gray, and bright white is bright gray.
  3. Dim bright colors (02;9#) are NOT the same as normal (3#) colors! (e.g. 31, 02;31, 91 and 02;91 all yield different results)
  4. Some color, effect, and/or reset codes may not work on all terminal emulators

Further Reading