Escape sequence notes

From Helpful
Jump to: navigation, search

Escape sequences generally refer to one of two things:

representing things indirectly

Exact set varies per language, but many have the following

Sorted roughly per reason

  • you specify this way because you cannot type them literally, e.g.
0x07 Alert (Beep, Bell) (added in C89)[1]
0x08 Backspace
0x0C Formfeed
0x0A Newline (Line Feed) (but see possible translation)
0x0D Carriage Return
0x09 Horizontal Tab
0x0B Vertical Tab
0x1B escape character (not always)
  • you specify this way because the context (delimiting and/or escaping syntax) makes it a special case, e.g.
0x27 Single quotation mark
0x22 Double quotation mark
0x5C Backslash
literal question mark, specifically in C, to avoid trigraphs
  • you specify this way because because you want to avoid the literal it implies - because it's clearer, leaves the file as ASCII, or such
hexadecimal number
octal number
, or similar for unicode

special meanings to text terminals

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

Typically known as ANSI escape codes. These days, look to ECMA-48, "Control Functions for Coded Character Sets", though note there are a bunch of privateish extensions to this in practice.

These start with the value 0x1B (033 in decimal, 27 in decimal), which is what the Esc key emits.

The length of the sequences varies with what's in there.

A lot of cases is ESC subset command, where the command implicitly terminates.
Some are delimited instead.

There is a loose ordering of types. For example,

  • DCS, "Device Control String"
Start with ESC P (0x1b 0x50)
things like fetching capabilities (verify)
  • OSC, "Operating System Command"
Start with ESC ] (0x1b 0x5d)
things like window title
  • CSI, "Control Sequence Introducer"
Start with ESC [ (0x1b 0x5b)
more expandable, e.g. with
-separated arguments
clear screen, cursor and control stuff
includes SGR, "Select Graphic Rendition"
which is typically used for color/bold/blink type stuff
all terminated by

See also:

ANSI color

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

These are a subset of SGR escapes (see also mention above)

Mostly in the form

ESC [ textspec m
where you will also see ESC mentioned as
(hex) or

Some of the more interesting and better-supported codes (search for Select Graphic Rendition)

  • 1: bright foreground color
bright variants can be shorthanded
  • 0 clears to default colors/no bright/underline(verify)
useful to start a sequence, because by default you only change the current state
also useful in itself, e.g. to stick at the end of a colored prompt
  • 4: set underline
  • selective clearing:
39 default foreground color (default color decided by the terminal itself(verify))
49 default foreground color
22 clears bold
24 clears underline

The classical set of colors:

  • 30 foreground: black (bright+black is how you get dark gray),
  • 31 foreground: red
  • 32 foreground: green
  • 33 foreground: yellow (non-bright yellow is brown or orange on some schemes),
  • 34 foreground: blue
  • 35 foreground: magenta
  • 36 foreground: cyan
  • 37 foreground: light grey (bright+lightgrey is how you get real white)
  • bright foreground colors can be shorthanded: 90..97 are effectively shorthand for 1;30 .. 1;37 (verify)
  • 40 background: black
  • 41 background: red
  • 42 background: green
  • 43 background: yellow
  • 44 background: blue
  • 45 background: magenta
  • 46 background: cyan
  • 47 background: light grey
note that bright applies only to foreground

Note that you can specify multiple codes at once, separated by
, before the

So, for example:

  • 35
    sets the foreground to magenta
  • 37;44
    is grey on blue
  • 1;37;44
    is white on blue
  • 1;33;45
    is bright yellow on magenta
  • 0;35
    clears foreground and background flags and colors, then sets the foreground to magenta
  • 0;1;35
    same but with bright magenta
  • 0;1;4;35
    same but also underlined
  • 1;47;30
    is grey (bright black) on white

To play around in a prompt, try something like:

printf "\x1b[0;1;33;45mtest\x1b[0m\n"

See also:

More colors

Modern terminals may also have 256-color and true color modes.

This is usually advertised via TERM, but there's a bunch of footnotes to that (TODO: write them up)

Assuming you've detecting it'll work, it's basically just another color-set-code with a new set of possible alues squeezed within the regular SGR
38 set foreground color with one of these newer codes
48 set background color with one of these newer codes

Followed by either

  • for 256 color':
    where n is
0..7: (like 30..37): black red green yellow blue magenta cyan gray
8..15: (like 90..97, the bright variants of the above)
16..231: 6×6×6 rgb cube. To calculate:
multiply red (range 0..5) by 36,
multiply green (range 0..5) by 6,
use blue (range 0..5) as-is
and add 16 to put it in this range
232..255: 24 shades of gray

For example, to test support you could do:

for i in {0..255} ; do
    printf "\x1b[38;5;${i}mcol${i}\x1b[0m\t"
done; echo

  • for truecolor:
where r, g, and b can each be 0..255

Supporting terms: seem to include xterm, konsole, libvte derived including gnome's


for i in {0..255} ; do
    printf "\x1b[38;2;100;100;${i}m100,100,${i}\x1b[0m\n"

What happens with these when then terminal doesn't support it?

In general, it will be ignored.

Some seem to try the closest supported color.

See also: