ANSI Escape Sequences

 ·  β˜• 4 min read

Introduction

The terminal can be controlled in all sorts of fun ways using Control Sequences.
This is not an enumeration of all options.
The ones explored here are part of the Control Sequence Introducer (CSI) set.

CSI starts with ESC[. (thus often called ’escape codes/sequences’)
ESC can be written in a few different forms: (oct and hex being the most widely supported)

  • \e
  • Ctrl-Key: ^[
  • Octal: \033
  • Unicode: \u001b
  • Hexadecimal: \x1B
  • Decimal: 27

CSI escape sequences take the form of ESC[<param> <char>.
<param> is a value or list of values separated by ;, <char> is some character that determines what the sequence is controlling, not all sequences have a space between <param> and <char>

In these CSI escape sequences, even the space character `` can be important to the function of the sequence. It will be mentioned if it’s required or not in each section.

It is possible to send these control sequences via echo. (-e flag tells echo to ignore any of the escape codes, thus the control sequence will be printed as is)

Text

Perhaps the most commonly encountered use is to modify text. e.g. color, weight, etc.

They use a <char> of m.

<param> can have multiple values separated by ;, the order is not important, but if the params conflict then the last one is used.

Values for param

Style

StyleSetUnset
Normal / Total reset0
Bold122
Faint (decreased intensity)222
Italicized323
Underlined424
Doubly-underlined2124
Blink (slow)525
Blink (fast)625
Invert fore- and background colours727
Invisible828
Crossed-out929

Colours

8 + 16 Colours:

(if you have only 8 colours, then ignore the Bright ones)

ColorForeground [Dim / Bright]Background [Dim / Bright]
Black30 / 9040 / 100
Red31 / 9141 / 101
Green32 / 9242 / 102
Yellow33 / 9343 / 103
Blue34 / 9444 / 104
Magenta35 / 9545 / 105
Cyan36 / 9646 / 106
White37 / 9747 / 107
Default3949

256 Colours:

The following escape codes tell the terminal to use the given color ID:

  • Foreground => ESC[38;5;{ID}m
  • Background => ESC[48;5;{ID}m

Where {ID} should be replaced with the color index from 0 to 255 of the following color table:

256 Color table

The table starts with the original 16 colors (0-15).

The proceeding 216 colors (16-231) are a flattened 3D matrix, increasing in blue, then green, then red.

The final 24 colors (232-255) are gray-scale ranging from just above black to just below white.

RGB Colors:

More modern terminals support 24-bit RGB (aka Truecolor), which can accept 0-255 values for each colour channel individually.

  • Foreground => ESC[38;2;{r};{g};{b}m
  • Background => ESC[48;2;{r};{g};{b}m

Note: ;38 and ;48 are for the higher-bit colour modes, and ;2 and ;5 sets the color format (256 colours or True Colour).

Example

To set the background yellow, the foreground bright purple, crossed out and blinking

\033[43;95;9;5mExample Text

Other notes

Terminals may implement blinking differently, some work by making the text faint periodically. This is done regardless of if the text is already faint (and thus blinking may be invisible on faint text).

ESC[6m (i.e. fast blinking) is not actually in the standard and so terminals may interpret this as either faster blinking, normal blinking, or do nothing.

Many terminals show the bright colors if the text is made Bold, even though Bold text and Bright colours are technically different control sequences.

Cursor Shape

It is possible to set the ‘shape’ of the cursor

For this sequence, the value of <char> is q.

The space is important for this sequence.

Values for param

  • `` β‡’ reset.
  • 0 β‡’ reset.
  • 1 β‡’ blinking block.
  • 2 β‡’ steady block.
  • 3 β‡’ blinking underline.
  • 4 β‡’ steady underline.
  • 5 β‡’ blinking bar.
  • 6 β‡’ steady bar.

Change Visibility

Although non-standard, some terminal emulators also allow you to change the visibility of the cursor.
This uses a different sequence and no <char> or space.

Make cursor invisible => ESC[?25l
Make cursor invisible => ESC[?25h

Example

To set the cursor to a blinking bar:

\033[5 q

Additional Information

A good summary of additional information (and source for some of this article) in this gist.

Full technical docs are here.


Kieran Goldsworthy
WRITTEN BY
Kieran Goldsworthy
Cloud Engineer and Architect


What's on this Page