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:
\x1b[1;31;40mBold Red on Black\x1b[0m → Bold (1) + Red Text (31) + Black BG (40)
| 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 |
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 | |||
Extended 8-bit palette. Every swatch displays its value directly for easy copying—click any cell to copy instantly, or double-click to select.
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 |
| 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 |
#include <stdio.h>
/* ANSI Escape Definitions */
#define ANSI_RESET "\x1b[0m"
#define ANSI_BOLD "\x1b[1m"
#define ANSI_DIM "\x1b[2m"
#define ANSI_RED "\x1b[31m"
#define ANSI_GREEN "\x1b[32m"
#define ANSI_YELLOW "\x1b[33m"
#define ANSI_BLUE "\x1b[34m"
int main(void) {
printf("%s%s[OK]%s Server listening on port 8080\n",
ANSI_BOLD, ANSI_GREEN, ANSI_RESET);
printf("%s%s[WARN]%s Request latency exceeded 200ms\n",
ANSI_BOLD, ANSI_YELLOW, ANSI_RESET);
printf("%s%s[ERR]%s Connection refused: 127.0.0.1:5432\n",
ANSI_BOLD, ANSI_RED, ANSI_RESET);
return 0;
}
# 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"
# Standard formatting
print("\033[1;36m==>\033[0m \033[1mDeploying release v1.4...\033[0m")
print("\033[1;32m✓\033[0m Connected to production database")
# 256-color & Truecolor
print("\033[38;5;141m[DEBUG]\033[0m Worker pool initialized")
print("\033[38;2;255;100;50m[TRUECOLOR]\033[0m Rendered gradient text\033[0m")