ANSI Escape Codes

Reference for ANSI terminal escape sequences — cursor, erase, mode and more.

Searchable ANSI/VT100 escape sequence reference covering CSI cursor moves, erase commands, SGR text modes, screen buffer and OSC sequences, with copyable literal bytes and examples. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

What is the ESC character in an ANSI sequence?

ESC is the escape control character, decimal 27 or hex 0x1B. Most ANSI sequences start with ESC followed by '[' to form a Control Sequence Introducer (CSI). In code it is often written as \e, \033 or \x1b.

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:

SequenceActionExample
ESC[{n}AMove cursor up n rowsESC[3A — up 3
ESC[{n}BMove cursor down n rowsESC[1B — down 1
ESC[{n}CMove cursor forward n columnsESC[5C
ESC[{n}DMove cursor back n columnsESC[2D
ESC[{row};{col}HMove cursor to absolute positionESC[1;1H — top-left
ESC[2JErase entire screen(clears display)
ESC[KErase to end of line(clear rest of current line)
ESC[0mReset all SGR attributesAlways end colored output with this
ESC[1mBold / bright
ESC[4mUnderline
ESC[38;5;{n}mSet foreground to 256-color code n
ESC[48;5;{n}mSet background to 256-color code n
ESC[38;2;{r};{g};{b}mSet foreground to 24-bit RGBTrue-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;;\a renders 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.