ANSI Control Sequences¶
ANSI Control Sequences.
These are used to control things in the terminal, like terminal text color, background colors, cursor visibility, screen clearing, mouse tracking, etc.
They're very useful for customizing how a terminal behaves under certain circumstances.
For colors, see text formatting sequences.
Types of Sequences¶
There are a few different types of ANSI control sequences:
ESC
: Escape Sequences. Start withESC
(e.g.,\033
,\x1b
)CSI
: Control Sequence Introducer. Starts withESC [
, or CSI (\x9B
)DCS
: Device Control String. Starts withESC P
, or DSC (\x90
)-
OSC
: Operating System Command. Starts withESC ]
, or OSC )\x9D
- (ignore whitespace between
ESC
and[
/P
/]
)
- (ignore whitespace between
Below is how you'd perform the ANSI control sequences (where \x1b
is ESC
):
\x1b # Basic escape sequence
\x1b[ # Control sequence introducer
\x1bP # Device Control String
\x1b] # Operating System Command
Also see ANSI escape sequence syntax.
ANSI Escape Sequence Syntax¶
You can start escape sequences a number of different ways.
These different methods of starting ANSI control sequences are called "Control Sequence Introducer," or "CSI" commands.
- Standard ANSI escape sequences are prefixed with
ESC[
, whereESC
is one of:\x1b
: Hexadecimal forESC
.\033
: Octal forESC
.\u001b
: Unicode forESC
.(
^[
): The actual escape control character.- Typing the characters
^
and[
will not work for this. It needs to be the actualCtrl-[
key signal.
- Typing the characters
\e
: Interpreted asESC
by many programs.\e
is not guaranteed to work in all languages/compilers.- It's recommended to use octal, hexadecimal, or unicode for
ESC
. - Check what's supported in your language/environment.
\e # Interpreted as ESC by programs
\033 # ESC in (Octal)
\x1b # ESC in (Hexadecimal)
\u001b # ESC in (Unicode)
^[ # ESC (the actual ESC control character, not the ^ and [ characters)
Any ESC
on this page can safely be replaced by \033
, \x1b
, or \u001b
.
You can use or
\e
(if supported).
The general syntax for an ANSI escape sequence is:
printf "\e[SEQ"
# or
printf "\033[SEQ"
# or
printf "[SEQ"
# or
printf "\x1b[SEQ"
SEQ
is the ANSI sequence that you're trying to access.
Some ANSI control sequences take multiple arguments. Specify multiple arguments by
using semicolons (;
) in between them.
For example, using a 255-color escape sequence:
"\x1b[38;5;33m"
38
: First arg5
: Second arg33
: Third argm
: The end
ASCII Codes¶
The ASCII codes here are used in some of the ANSI control sequences.
Name | Decimal | Octal | Hex | C Escape | Ctrl-Key | What it is |
---|---|---|---|---|---|---|
BEL |
7 |
007 |
0x07 |
\a |
^G |
Terminal bell |
BS |
8 |
010 |
0x08 |
\b |
^H |
Backspace |
HT |
9 |
011 |
0x09 |
\t |
^I |
Horizontal tab |
LF |
10 |
012 |
0x0A |
\n |
^J |
Line feed (newline) |
VT |
11 |
013 |
0x0B |
\v |
^K |
Vertical tab |
FF |
12 |
014 |
0x0C |
\f |
^L |
Form feed (also: New page NP) |
CR |
13 |
015 |
0x0D |
\r |
^M |
Carriage return (CR) |
ESC |
27 |
033 |
0x1B |
\e |
^[ |
Escape character |
DEL |
127 |
177 |
0x7F |
<none> |
<none> |
Delete character |
Text Formatting Sequences (SGR)¶
SGR - Select Graphic Rendition. This is used to format text.
The ANSI color control sequences are used to change the text color
and background color of the terminal.
Each number in the control sequence represents a way to customize the foreground (text) or background.
See the wiki article for a list of SGR parameters.
Color Basic Syntax¶
The basic syntax for an ANSI color control sequence is:
printf "\e[<STYLE_CODE>m"
\e
represents the escape character, which begins the sequence.<STYLE_CODE>
is a number (or series of numbers separated by semicolons) that specifies the color or style.m
indicates the end of the sequence.
Quickref¶
Colors:
Number | Color |
---|---|
0 |
black |
1 |
red |
2 |
green |
3 |
yellow |
4 |
blue |
5 |
magenta |
6 |
cyan |
7 |
white |
ANSI codes:
Number | Description |
---|---|
0 | clear |
1 | bold |
4 | underline |
5 | blink |
30-37 | fg color |
40-47 | bg color |
1K | clear line (to beginning of line) |
2K | clear line (entire line) |
2J | clear screen |
0;0H | move cursor to 0;0 |
1A | move up 1 line |
Bash Utilities:
hide_cursor() { printf "\e[?25l"; }
show_cursor() { printf "\e[?25h"; }
Common Style Codes¶
-
Text Styles and Defaults
0
: Reset all styles to default.1
: Bold.4
: Underline.5
: Blink (not widely supported).7
: Inverse (swap foreground and background colors).39
: Default foreground (text) color.49
: Default background color.
-
Standard Text Colors (30-37, 90-97)
30
to37
: Standard foreground colors.40
to47
: Standard background colors.90
to97
: Bright (or bold) mode for 16-color foreground colors.100
to107
: Bright (or bold) mode for 16-color background colors.
-
256-Color Mode
38;5;<COLOR_CODE>
: Set the foreground color in 256-color mode.48;5;<COLOR_CODE>
: Set the background color in 256-color mode.<COLOR_CODE>
ranges from0
to255
.
Standard Colors (8 color)¶
The foreground (30-37
) and background (40-47
) colors use 8 "standard" colors.
The 8 standard colors are:
0
: Black1
: Red2
: Green3
: Yellow4
: Blue5
: Magenta6
: Cyan7
: White
Formatting the Style Code¶
The style code can be formatted in several ways.
As long as the numbers given match the ones above, the order doesn't matter
all that much.
I generally format the style code like this:
printf "\e[<TEXT_STYLE>;<COLOR_MODE>;<COLOR>]"
<TEXT_STYLE>
is a number that specifies the text style (1
-7
or0
).- There can be multiple text styles, separated by semicolons.
<COLOR_MODE>
will specify the color mode (i.e., if using 256-color).- This can be
38;5
or48;5
for 256-color mode foreground and background respectively.
- This can be
ANSI Color Examples¶
printf "\e[1;5;31;40m---Testing a color code---\e[0m"
\e[
: The start of the sequence- This can also be
\033[
- This can also be
1;5;
: Specifies bold and blinking text (styles1-7
))31;
: Specifies red text (foreground30-37
)40
: Specifies grey/black background (background40-47
)-
m
: The end of the sequence -
\e[0m
: Resets all styles to default.
256-colors¶
In an ANSI control sequence using SGR, the control sequence can take multiple arguments to specify 256-color.
"\x1b[38;5;196m"
ESC[
: CSI38;
: indicates "foreground"5;
: indicates "256-color"33
: A number in the range0
-255
.m
: The end of the control sequence.
SYNTAX="\e[38;5;${COLOR_CODE}m"
# or, if used in your prompt customization:
PS1_SYNTAX="\[\e[38;5;${COLOR_CODE}m\]"
# Boils down to this:
"ESC[38;5;${COLOR_CODE}m" # Select foreground color
"ESC[48;5;${COLOR_CODE}m" # Select background color
The ESC
is one of the escape methods listed here.
The ${COLOR_CODE}
is a number between 0
and 255
:
0-7
: Standard colors (Same asESC[30-37m
)8-15
: High intensity colors (Same asESC[90-97m
)16-231
: 6x6x6 RGB cube232-255
: Grayscale in 24 steps.
Chaining Colors¶
You can specify both a foreground and background color in the same escape sequence.
An example:
printf "\x1b[38;5;196;48;5;227mHello, world\x1b[0m\n"
\x1b[
: CSI (ESC[
)38;5;196
: Specify the foreground (38
)5;196
is selecting196
out of the 256 colors.
48;5;232
: Specify the background (48
)5;232
is selecting232
out of the 256 colors.
This prints the text Hello, world
with a black background with red text.
Then we have \x1b[0m
to reset the colors back to their defaults.
Terminal Manipulation Sequences¶
Cursor Manipulation¶
These sequences control the cursor's position on the screen.
Sequence | Action |
---|---|
ESC[nA |
Move cursor up by n lines |
ESC[nB |
Move cursor down by n lines |
ESC[nC |
Move cursor forward by n columns |
ESC[nD |
Move cursor backward by n columns |
ESC[nF |
Move cursor to beginning of line, n lines up |
ESC[nG |
Move cursor to column n |
ESC[n;fH |
Move cursor to row n , column f |
ESC[M |
Move cursor one line up. Scrolls if necessary |
ESC[6n |
Request cursor position (responds as ESC[n;nR ), (literal n ) |
ESC[7 |
Save cursor position (DEC) |
ESC[8 |
Restore cursor position (DEC) |
ESC[s |
Save cursor position (SCO) |
ESC[u |
Restore cursor position (SCO) |
- DEC v. SCO
- Some terminals (
xterm
-derived) support both SCO and DEC sequences, but they may have different functionality. - Prefer DEC sequences over SCO.
- Some terminals (
You can also change the cursor's shape and behavior.
Sequence | Description |
---|---|
ESC[0 q |
Changes cursor shape to steady block |
ESC[1 q |
Changes cursor shape to steady block also |
ESC[2 q |
Changes cursor shape to blinking block |
ESC[3 q |
Changes cursor shape to steady underline |
ESC[4 q |
Changes cursor shape to blinking underline |
ESC[5 q |
Changes cursor shape to steady bar |
ESC[6 q |
Changes cursor shape to blinking bar |
ESC[?25l |
Hide the cursor |
ESC[?25h |
Show the cursor (unhide cursor) |
E.g.,:
printf "\x1b[\x30 q" # changes to blinking block
printf "\x1b[\x31 q" # changes to blinking block
printf "\x1b[\x32 q" # changes to steady block
printf "\x1b[\x33 q" # changes to blinking underline
printf "\x1b[\x34 q" # changes to steady underline
printf "\x1b[\x35 q" # changes to blinking bar
printf "\x1b[\x36 q" # changes to steady bar
printf "\x1b[?25l" # will hide cursor
printf "\x1b[?25h" # will show cursor again
Screen and Line Clearing¶
These sequences clear parts or all of the terminal screen.
Sequence | Action |
---|---|
ESC[J |
Clear from cursor to end of the screen (same as ESC[0J ) |
ESC[0J |
Clear from cursor to end of the screen |
ESC[1J |
Clear from cursor to beginning of the screen |
ESC[2J |
Clear entire screen |
ESC[3J |
Clear saved lines |
ESC[K |
Clear from cursor to end of line (same as ESC[0K ) |
ESC[0K |
Clear from cursor to end of line |
ESC[1K |
Clear from cursor to beginning of line |
ESC[2K |
Clear entire line |
Clearing lines won't move the cursor. The cursor will stay where it was before clearing.
Use \r
to move the cursor to the beginning of the line if you need to.
Also see cursor movement.
Mouse Interaction¶
Some terminals support mouse tracking sequences:
Sequence | Action |
---|---|
ESC[?1000h |
Enable mouse tracking |
ESC[?1000l |
Disable mouse tracking |
Operating System Commands (OSC)¶
These sequences interact with the terminal emulator's operating system features.
Sequence | Action |
---|---|
ESC]0;titleBEL |
Set window title to title |
ESC]2;titleBEL |
Set icon name to title |
BEL
is the bell character (\a
or \x07
).
Private Modes¶
These are implemented in most terminals, though not explicitly defined in the ANSI specification.
Sequence | Description |
---|---|
ESC[?25l |
Make cursor invisible |
ESC[?25h |
Make cursor visible |
ESC[?47l |
Restore screen |
ESC[?47h |
Save screen |
ESC[?1049h |
Enables the alternative buffer |
ESC[?1049l |
Disables the alternative buffer |
ESC[?1004h |
Enable reporting focus. Reports whenever terminal enters/exits focus (ESC[I /ESC[O respectively. |
ESC[?1004l |
Disable reporting focus. |
ESC[ s |
Save current cursor position (SCP, SCOSC). |
ESC[ u |
Restore saved cursor position (RCP, SCORC). |
Resources¶
- https://jvns.ca/blog/2025/03/07/escape-code-standards
- https://en.wikipedia.org/wiki/ANSI_escape_code
- https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
- https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797#screen-modes
- https://stackoverflow.com/questions/4416909/anyway-change-the-cursor-vertical-line-instead-of-a-box
- https://invisible-island.net/xterm/ctlseqs/ctlseqs.html