Control the terminal with escape sequences
ANSI (also called VT100) escape sequences are short byte strings that tell a
terminal to move the cursor, clear regions, change text style, switch screen
buffers, or set the window title. They all start with the ESC control character
(decimal 27, hex 0x1B, written \e or \033 in most languages).
This reference groups the common sequences by purpose and lets you copy each one
with the real ESC byte already in place.
The four sequence families
CSI — Control Sequence Introducer (ESC [)
The workhorse of terminal control. Every CSI sequence is ESC [ followed by optional numeric parameters separated by semicolons, then a terminating letter:
| Sequence | Action | Example |
|---|---|---|
ESC[{n}A | Move cursor up n rows | ESC[3A — up 3 |
ESC[{n}B | Move cursor down n rows | ESC[1B — down 1 |
ESC[{n}C | Move cursor forward n columns | ESC[5C |
ESC[{n}D | Move cursor back n columns | ESC[2D |
ESC[{row};{col}H | Move cursor to absolute position | ESC[1;1H — top-left |
ESC[2J | Erase entire screen | (clears display) |
ESC[K | Erase to end of line | (clear rest of current line) |
ESC[0m | Reset all SGR attributes | Always end colored output with this |
ESC[1m | Bold / bright | |
ESC[4m | Underline | |
ESC[38;5;{n}m | Set foreground to 256-color code n | |
ESC[48;5;{n}m | Set background to 256-color code n | |
ESC[38;2;{r};{g};{b}m | Set foreground to 24-bit RGB | True-color terminals only |
SGR — Select Graphic Rendition
SGR sequences (ESC[...m) change text appearance. Multiple attributes can be combined with semicolons: ESC[1;4;31m sets bold, underline, and red foreground simultaneously. Always reset with ESC[0m to avoid leaking attributes into subsequent output.
OSC — Operating System Command (ESC ])
OSC sequences are for communication with the terminal emulator itself rather than the text display:
- OSC 0 / OSC 2: set the window title —
ESC]2;My Title\a - OSC 8: hyperlinks —
ESC]8;;https://example.com\aclick here\ESC]8;;\arenders a clickable link in supporting terminals (iTerm2, Kitty, recent GNOME Terminal versions).
OSC sequences end with BEL (\a, 0x07) or a string terminator (ESC \).
DEC private sequences
Prefix ESC[? for DEC private modes. The two most common:
ESC[?25l/ESC[?25h— hide / show cursor (useful for TUI apps)ESC[?1049h/ESC[?1049l— enter / exit the alternate screen buffer
How it works
When you copy a sequence from this reference, the \e placeholder is replaced with the actual ESC byte (0x1B). The numeric placeholders {n}, {row}, {col} are values you substitute; omitting a number usually defaults it to 1.
Practical examples
# Clear screen and home cursor
printf '\e[2J\e[H'
# Print bold red text then reset
printf '\e[1;31mError:\e[0m something went wrong\n'
# Move cursor to row 5, col 10
printf '\e[5;10H'
# Hide cursor during a progress bar, restore after
printf '\e[?25l'
# ... your progress output ...
printf '\e[?25h'
In bash, ANSI-C quoting ($'\e[...') or printf with \033 both work reliably across shells. Avoid using raw echo -e in scripts, as its behaviour varies between systems.