ANSI terminal styling codes
---------------------------
You probably are wondering how some pages or plain text files have[32m
colored[0m or[1m bold[0m text if you view them in the terminal. That's
called terminal SGR codes (short for Select Graphic Rendition). Most of them
are standardized by ANSI and implemented in lots of modern terminal
emulators. Sometimes they are called "escape codes" because they all start
with the actual escape character (further referred to as ESC).
The ESC character (0x1B in hex) can be input in different ways. In NeoVim,
it's just Ctrl+V Esc. Note that it can visually appear as ^[, but all the
following codes actually require entering another [, so keep that in mind.
In fact, since I'm only going to cover the styling codes, all of them share
the same structure: `ESC[(number)m`. Styling can actually be combined by
joining numbers with a semicolon (;), e.g. ESC[31;1m will set red AND bold
font at the same time. With that said, let me list all the main terminal
mode numeric codes here:
* 0: reset all styling, all modes off (don't ever forget to add ESC[0m at the
end of your text, be nice to viewers!)
* 1: bold mode on
* 2: faint mode on
* 22: bold/faint mode off
* 3/23: italic mode on/off
* 4/24: underline mode on/off
* 5/25: blinking mode on/off
* 7/27: inverse mode on/off
* 8/28: hidden mode on/off (the text is invisible but present and you can
copy it from the terminal, for instance)
* 9/29: strikethrough mode on/off
Note that some text display modes, like italic, underline, strikethrough or
blinking, might not be supported on all terminal emulators today. They will
do nothing if these codes are applied, and you should expect some of your
viewers to not see this text in [3mitalic[23m, [4munderlined[24m,
[9mstrikethrough[29m or even [5mblinking[25m style.
And now, the main color codes that can be combined with this modes too (the
values are specified for the foreground/for the background):
* Black: 30/40
* Red: 31/41
* Green: 32/42
* Yellow: 33/43
* Blue: 34/44
* Magenta: 35/45
* Cyan: 36/46
* White: 37/47
* Extended codes: 38/48 (more on that later)
* Default (reset the color without resetting the styling): 39/49
The color codes from 30 to 37 and from 40 to 47 are ancient, basic and work
everywhere. Same for the codes 39 and 49. When combined with the bold
attribute, the palette usually changes for brighter tints, so this is
considered a 16-color palette even though it only has 8 basic colors.
Now, the codes 38 and 48 allow you to specify advanced palette used by
somewhat more modern terminal emulators. For example, the ones that work in
the xterm-256color mode support the combinations ESC[38;5;(ID)m for the
foreground and ESC[48;5;(ID)m for the background, where the (ID) is a number
from 0 to 256. Even more modern terminals (and you can check this by
checking their $COLORTERM environment variable for "truecolor" or "24bit"
value) support setting the colors using full RGB palette! And the
corresponding sequences to do this are:
where (r), (g) and (b) are the numbers from 0 to 255 for the red, green and
blue components respectively.
There you have your full coloring (8/16-color, 256-color and even RGB) and
all main text modes (bold, italic, underline, strikethrough, inverse, hidden
and even blinking) right in your terminal... so why do you even need Web?