xterm.codes Developer Reference

ANSI Escape Codes

A quick reference for 3-bit, 4-bit, 8-bit (256-color), and 24-bit (Truecolor) terminal styling.

Escape Syntax:
256-Color Mode:
Active Format: \x1b[38;5;Nm (Hex Foreground)

1. Syntax & Anatomy

escape sequence structure

ANSI escape sequences control text formatting, colors, and cursor positioning in Unix and terminal environments. Every sequence begins with an Escape character followed by a Control Sequence Introducer (CSI), zero or more parameter codes separated by semicolons, and a terminating character.

Representation Example Sequence Environment / Language
Hexadecimal (\x1b) \x1b[31m C, C++, Python, JavaScript/Node.js, Rust
Octal (\033) \033[31m POSIX Shell, C, Perl, Ruby, Awk
Escape shorthand (\e) \e[31m Bash, Zsh, GNU echo -e, GNU printf
ASCII Control (ESC) ^[ [ 31m Raw terminal input (Ctrl+[ or ASCII 27)

Select Graphic Rendition (SGR): Color and styling sequences always terminate with m. Parameters can be combined into a single sequence with semicolons:

SGR chaining & reset

# Format: <esc>[<attribute>;<foreground>;<background>m

\x1b[1;31;40mBold Red on Black\x1b[0m → Bold (1) + Red Text (31) + Black BG (40)

Δ Tip: Click any code token, table cell, or swatch on this page to copy its escape sequence to your clipboard.

2. Text Attributes

styles & weights
Code Effect Reset Code Notes / Support
\x1b[0m Reset / Normal Turns off all styles, colors, and backgrounds
\x1b[1m Bold / High Intensity \x1b[22m Increases font weight; often renders colors brighter
\x1b[2m Dim / Faint \x1b[22m Decreases color intensity and luminosity
\x1b[3m Italic \x1b[23m Slanted/italic typeface (widely supported in modern terminals)
\x1b[4m Underline \x1b[24m Standard single underline beneath text
\x1b[5m Slow Blink \x1b[25m Less than 150 blinks/min (disabled or replaced with bold in many terminals)
\x1b[7m Inverse / Reverse \x1b[27m Swaps foreground and background color
\x1b[8m Conceal / Hidden \x1b[28m Hides characters (often used for password inputs)
\x1b[9m Strikethrough \x1b[29m Draws a horizontal line through text

3. Standard 16 Colors

3-bit & 4-bit palette

The foundation of terminal color output. Standard colors (codes 30–37 FG, 40–47 BG) and high-intensity bright variants (codes 90–97 FG, 100–107 BG). Values update with your selected syntax above.

Color Standard FG Standard BG Standard Preview Bright FG Bright BG Bright Preview
Black \x1b[30m \x1b[40m Black \x1b[90m \x1b[100m Bright
Red \x1b[31m \x1b[41m Red \x1b[91m \x1b[101m Bright
Green \x1b[32m \x1b[42m Green \x1b[92m \x1b[102m Bright
Yellow \x1b[33m \x1b[43m Yellow \x1b[93m \x1b[103m Bright
Blue \x1b[34m \x1b[44m Blue \x1b[94m \x1b[104m Bright
Magenta \x1b[35m \x1b[45m Magenta \x1b[95m \x1b[105m Bright
Cyan \x1b[36m \x1b[46m Cyan \x1b[96m \x1b[106m Bright
White \x1b[37m \x1b[47m White \x1b[97m \x1b[107m Bright
Default / Reset \x1b[39m \x1b[49m Resets foreground or background back to default terminal color

4. 256-Color Palette (8-Bit)

xterm color lookup

Extended 8-bit palette. Every swatch displays its value directly for easy copying—click any cell to copy instantly, or double-click to select.

Escape Syntax:
256-Color Mode:
Active Format: \x1b[38;5;Nm (Hex Foreground)

System Colors (Codes 0–15)

0–7 Standard • 8–15 High-Intensity
\x1b[38;5;0m
\x1b[38;5;1m
\x1b[38;5;2m
\x1b[38;5;3m
\x1b[38;5;4m
\x1b[38;5;5m
\x1b[38;5;6m
\x1b[38;5;7m
\x1b[38;5;8m
\x1b[38;5;9m
\x1b[38;5;10m
\x1b[38;5;11m
\x1b[38;5;12m
\x1b[38;5;13m
\x1b[38;5;14m
\x1b[38;5;15m

6×6×6 RGB Cube (Codes 16–231)

216 colors

Index formula: 16 + (36 × R) + (6 × G) + B where R, G, B ∈ [0, 5]. Channel values: 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff.

Slice R = 0 (0x00) Codes 16 – 51
\x1b[38;5;16m
\x1b[38;5;17m
\x1b[38;5;18m
\x1b[38;5;19m
\x1b[38;5;20m
\x1b[38;5;21m
\x1b[38;5;22m
\x1b[38;5;23m
\x1b[38;5;24m
\x1b[38;5;25m
\x1b[38;5;26m
\x1b[38;5;27m
\x1b[38;5;28m
\x1b[38;5;29m
\x1b[38;5;30m
\x1b[38;5;31m
\x1b[38;5;32m
\x1b[38;5;33m
\x1b[38;5;34m
\x1b[38;5;35m
\x1b[38;5;36m
\x1b[38;5;37m
\x1b[38;5;38m
\x1b[38;5;39m
\x1b[38;5;40m
\x1b[38;5;41m
\x1b[38;5;42m
\x1b[38;5;43m
\x1b[38;5;44m
\x1b[38;5;45m
\x1b[38;5;46m
\x1b[38;5;47m
\x1b[38;5;48m
\x1b[38;5;49m
\x1b[38;5;50m
\x1b[38;5;51m
↓ Green: rows 0 to 5 Blue: cols 0 to 5 →
Slice R = 1 (0x5f) Codes 52 – 87
\x1b[38;5;52m
\x1b[38;5;53m
\x1b[38;5;54m
\x1b[38;5;55m
\x1b[38;5;56m
\x1b[38;5;57m
\x1b[38;5;58m
\x1b[38;5;59m
\x1b[38;5;60m
\x1b[38;5;61m
\x1b[38;5;62m
\x1b[38;5;63m
\x1b[38;5;64m
\x1b[38;5;65m
\x1b[38;5;66m
\x1b[38;5;67m
\x1b[38;5;68m
\x1b[38;5;69m
\x1b[38;5;70m
\x1b[38;5;71m
\x1b[38;5;72m
\x1b[38;5;73m
\x1b[38;5;74m
\x1b[38;5;75m
\x1b[38;5;76m
\x1b[38;5;77m
\x1b[38;5;78m
\x1b[38;5;79m
\x1b[38;5;80m
\x1b[38;5;81m
\x1b[38;5;82m
\x1b[38;5;83m
\x1b[38;5;84m
\x1b[38;5;85m
\x1b[38;5;86m
\x1b[38;5;87m
↓ Green: rows 0 to 5 Blue: cols 0 to 5 →
Slice R = 2 (0x87) Codes 88 – 123
\x1b[38;5;88m
\x1b[38;5;89m
\x1b[38;5;90m
\x1b[38;5;91m
\x1b[38;5;92m
\x1b[38;5;93m
\x1b[38;5;94m
\x1b[38;5;95m
\x1b[38;5;96m
\x1b[38;5;97m
\x1b[38;5;98m
\x1b[38;5;99m
\x1b[38;5;100m
\x1b[38;5;101m
\x1b[38;5;102m
\x1b[38;5;103m
\x1b[38;5;104m
\x1b[38;5;105m
\x1b[38;5;106m
\x1b[38;5;107m
\x1b[38;5;108m
\x1b[38;5;109m
\x1b[38;5;110m
\x1b[38;5;111m
\x1b[38;5;112m
\x1b[38;5;113m
\x1b[38;5;114m
\x1b[38;5;115m
\x1b[38;5;116m
\x1b[38;5;117m
\x1b[38;5;118m
\x1b[38;5;119m
\x1b[38;5;120m
\x1b[38;5;121m
\x1b[38;5;122m
\x1b[38;5;123m
↓ Green: rows 0 to 5 Blue: cols 0 to 5 →
Slice R = 3 (0xaf) Codes 124 – 159
\x1b[38;5;124m
\x1b[38;5;125m
\x1b[38;5;126m
\x1b[38;5;127m
\x1b[38;5;128m
\x1b[38;5;129m
\x1b[38;5;130m
\x1b[38;5;131m
\x1b[38;5;132m
\x1b[38;5;133m
\x1b[38;5;134m
\x1b[38;5;135m
\x1b[38;5;136m
\x1b[38;5;137m
\x1b[38;5;138m
\x1b[38;5;139m
\x1b[38;5;140m
\x1b[38;5;141m
\x1b[38;5;142m
\x1b[38;5;143m
\x1b[38;5;144m
\x1b[38;5;145m
\x1b[38;5;146m
\x1b[38;5;147m
\x1b[38;5;148m
\x1b[38;5;149m
\x1b[38;5;150m
\x1b[38;5;151m
\x1b[38;5;152m
\x1b[38;5;153m
\x1b[38;5;154m
\x1b[38;5;155m
\x1b[38;5;156m
\x1b[38;5;157m
\x1b[38;5;158m
\x1b[38;5;159m
↓ Green: rows 0 to 5 Blue: cols 0 to 5 →
Slice R = 4 (0xd7) Codes 160 – 195
\x1b[38;5;160m
\x1b[38;5;161m
\x1b[38;5;162m
\x1b[38;5;163m
\x1b[38;5;164m
\x1b[38;5;165m
\x1b[38;5;166m
\x1b[38;5;167m
\x1b[38;5;168m
\x1b[38;5;169m
\x1b[38;5;170m
\x1b[38;5;171m
\x1b[38;5;172m
\x1b[38;5;173m
\x1b[38;5;174m
\x1b[38;5;175m
\x1b[38;5;176m
\x1b[38;5;177m
\x1b[38;5;178m
\x1b[38;5;179m
\x1b[38;5;180m
\x1b[38;5;181m
\x1b[38;5;182m
\x1b[38;5;183m
\x1b[38;5;184m
\x1b[38;5;185m
\x1b[38;5;186m
\x1b[38;5;187m
\x1b[38;5;188m
\x1b[38;5;189m
\x1b[38;5;190m
\x1b[38;5;191m
\x1b[38;5;192m
\x1b[38;5;193m
\x1b[38;5;194m
\x1b[38;5;195m
↓ Green: rows 0 to 5 Blue: cols 0 to 5 →
Slice R = 5 (0xff) Codes 196 – 231
\x1b[38;5;196m
\x1b[38;5;197m
\x1b[38;5;198m
\x1b[38;5;199m
\x1b[38;5;200m
\x1b[38;5;201m
\x1b[38;5;202m
\x1b[38;5;203m
\x1b[38;5;204m
\x1b[38;5;205m
\x1b[38;5;206m
\x1b[38;5;207m
\x1b[38;5;208m
\x1b[38;5;209m
\x1b[38;5;210m
\x1b[38;5;211m
\x1b[38;5;212m
\x1b[38;5;213m
\x1b[38;5;214m
\x1b[38;5;215m
\x1b[38;5;216m
\x1b[38;5;217m
\x1b[38;5;218m
\x1b[38;5;219m
\x1b[38;5;220m
\x1b[38;5;221m
\x1b[38;5;222m
\x1b[38;5;223m
\x1b[38;5;224m
\x1b[38;5;225m
\x1b[38;5;226m
\x1b[38;5;227m
\x1b[38;5;228m
\x1b[38;5;229m
\x1b[38;5;230m
\x1b[38;5;231m
↓ Green: rows 0 to 5 Blue: cols 0 to 5 →

Grayscale Ramp (Codes 232–255)

24 shades

Formula: gray = 8 + (10 × (n - 232)) for values from 8 (#080808) to 238 (#eeeeee).

\x1b[38;5;232m
\x1b[38;5;233m
\x1b[38;5;234m
\x1b[38;5;235m
\x1b[38;5;236m
\x1b[38;5;237m
\x1b[38;5;238m
\x1b[38;5;239m
\x1b[38;5;240m
\x1b[38;5;241m
\x1b[38;5;242m
\x1b[38;5;243m
\x1b[38;5;244m
\x1b[38;5;245m
\x1b[38;5;246m
\x1b[38;5;247m
\x1b[38;5;248m
\x1b[38;5;249m
\x1b[38;5;250m
\x1b[38;5;251m
\x1b[38;5;252m
\x1b[38;5;253m
\x1b[38;5;254m
\x1b[38;5;255m

5. 24-Bit Truecolor (RGB)

16.7 million colors

Allows direct specification of 24-bit RGB values from 0 to 255. Supported by almost all modern terminal emulators (tmux, Alacritty, Kitty, iTerm2, macOS Terminal, Windows Terminal, xterm).

Target Sequence Format Example (Orange: 255, 128, 0)
Foreground <esc>[38;2;<r>;<g>;<b>m \x1b[38;2;255;128;0m
Background <esc>[48;2;<r>;<g>;<b>m \x1b[48;2;20;30;45m
Combined <esc>[38;2;r;g;b;48;2;r;g;bm \x1b[38;2;255;255;255;48;2;255;128;0m

6. Terminal Controls

cursor & screen sequences
Sequence Action Description
\x1b[0m Reset All Reset all attributes, foreground, and background colors
\x1b[2J Clear Screen Clears the entire visible screen
\x1b[H Cursor Home Moves cursor to row 1, column 1 (upper-left corner)
\x1b[<r>;<c>H Move Cursor Moves cursor to specific 1-indexed row <r> and column <c>
\x1b[<n>A Cursor Up Moves cursor up by <n> rows
\x1b[<n>B Cursor Down Moves cursor down by <n> rows
\x1b[<n>C Cursor Forward Moves cursor forward (right) by <n> columns
\x1b[<n>D Cursor Back Moves cursor backward (left) by <n> columns
\x1b[2K Clear Line Clears the entire current line
\x1b[0K Clear to Line End Clears from cursor position to the end of the line
\x1b[?25l Hide Cursor Hides terminal cursor (useful in TUI applications)
\x1b[?25h Show Cursor Restores terminal cursor visibility
\x1b[s Save Cursor Saves current cursor coordinates
\x1b[u Restore Cursor Restores previously saved cursor coordinates

7. Practical Code Snippets

interactive terminal examples
# Portable POSIX printf (uses octal \033)
printf "\033[1;32m✓ Success:\033[0m All test suites passed.\n"
printf "\033[1;31m✗ Error:\033[0m Verification failed at step 4.\n"

# 256-color foreground (color 208 = orange)
printf "\033[38;5;208mNotice: Cache rebuilt in 45ms\033[0m\n"

# 24-bit Truecolor (RGB: 255, 120, 0)
printf "\033[38;2;255;120;0mCustom RGB orange text\033[0m\n"