Terminal escape sequences

last modified

2023–9–17

Based on the Wikipedia article ANSI escape code. Only codes which seem useful and work (in Terminator) are included.

The escapes below can be used with bash using echo by activating expansion with the -e option (echo -e "\a"), using printf (printf "\a"), or in a string literal using ANSI-C Quoting ($'\a'). The same escapes can be used in Python strings, except that ESC has to be written as \x1b.

C0 control codes

\a alert ASCII 0x07 BEL
\b backspace ASCII 0x08 BS
\e escape ASCII 0x1b ESC
\f form feed ASCII 0x0c FF
\n new line ASCII 0x0a LF
\r carriage return ASCII 0x0d CR
\t horizontal tab ASCII 0x09 HT

Control Sequence Introducer sequences

The Control Sequence Introducer (CSI) is ESC-[.

Cursor movement and screen content

\e[nA move cursor n lines up
\e[nB move cursor n lines down
\e[nC move cursor n columns to the right
\e[nD move cursor n columns to the left
\e[nE move cursor n lines up, to the beginning of the line
\e[nF move cursor n lines down, to the beginning of the line
\e[nG move cursor to column n
\e[n;mH move cursor to line n, column m
\e[0J clear from cursor to end of screen
\e[1J clear from cursor to beginning of screen
\e[2J clear entire screen
\e[3J clear entire screen and scrollback buffer
\e[0K clear from cursor to end of line
\e[1K clear from cursor to beginning of line
\e[2K clear entire line
\e[nS scroll screen content up by n lines (blank lines at bottom)
\e[nT scroll screen content down by n lines (blank lines at top)

If the bash option checkwinsize is set, it checks the terminal size after each external command and sets the environment variables COLUMNS and LINES accordingly. The terminal size can also directly checked using stty size, which prints numbers of lines and columns separated by a space.

Select Graphic Rendition sequences

Select Graphic Rendition (SGR) sequences have the form ESC-[-parameter(s)-m. Multiple parameters are separated by a semicolon; for example the escape sequence for bold font and foreground palette color 214 is \e[1;38;5;214m.

parameter(s) effect
0 reset / normal
1 bold or bright
2 dim
3 italic
4 underlined
5 blink
7 reversed
8 hidden
9 struck through
21 double underlined
3037 standard foreground color (palette 0–7)
4047 standard background color (palette 0–7)
9097 bright foreground color (palette 8–15)
100107 bright background color (palette 8–15)
38;5;nm foreground color n (palette 0–255)
48;5;nm background color n (palette 0–255)
38;2;R;G;B RGB foreground color; R, G, B from 0 to 255
48;2;R;G;B RGB foreground color; R, G, B from 0 to 255
256-color palette
index color
0 standard black
1 standard red
2 standard green
3 standard yellow (brown)
4 standard blue
5 standard magenta
6 standard cyan
7 standard white (light gray)
8 bright black (dark gray)
9 bright red
10 bright green
11 bright yellow
12 bright blue
13 bright magenta
14 bright cyan
15 bright white
16–231 6 × 6 × 6 color cube, 16 + 36 × r + 6 × g + b
232–255 shades of gray from dark to bright

Palette RGB values are implementation- and possibly theme-dependent and may be configurable.

Appearance of the 256-color palette in Terminator with the ‘White on Black’ color scheme and the ‘Linux’ palette

Operating System Command sequences

Operating System Command (OSC) sequences start with ESC-]. Strings are ended by the String Terminator (ST) sequence ESC-\.

\e]0;title\e\\ set window title

See Operating System Commands.

OSC-133: Semantic prompts

\e]133;L\a move cursor to start of new line, unless it is already at the start of a line
\e]133;D;c\a end of current command, indicate exit code c
\e]133;A\a start a new command, and enter prompt mode
\e]133;B\a end of prompt and start of user input
\e]133;C\a end of input, and start of output

See Semantic prompts.