ANSI Escape Sequences: Terminal Colors and Cursor Control

ANSI escape sequences are in-band control codes — standardized by ECMA-48 in 1976 — that terminals interpret to set colors, move the cursor, and clear the screen. The Control Sequence Introducer (CSI = ESC [, bytes 0x1B 0x5B) starts most commands. SGR sequences ending in 'm' control colors: 3/4-bit (16 base), 8-bit (256 palette), and 24-bit truecolor (ESC [38;2;R;G;Bm). Windows ignored them until version 1511 in 2016. Writing escapes to a non-tty stream corrupts log files, so well-behaved tools check isatty() first.

ANSI escape sequences (or ANSI escape codes) are in-band control codes that text terminals and terminal emulators interpret to set colors, move the cursor, clear regions of the screen, and toggle text attributes such as bold or underline. Standardized by ECMA-48 in 1976 and later by ANSI X3.64 (1979) and ISO/IEC 6429 (1983), they grew out of vendor-specific sequences introduced by hardware terminals like the DEC VT100, which became the first popular implementation in 1978. Most sequences begin with the Control Sequence Introducer (CSI), the two bytes ``ESC [`` — that is, ``0x1B 0x5B`` (often written ``\033[``, ``\x1b[``, or ``\e[``). After the CSI come optional numeric parameters separated by semicolons, then a final byte that picks the command. ``ESC [ H`` moves the cursor to the home position, ``ESC [ 2J`` clears the entire screen, ``ESC [ s`` saves the cursor position and ``ESC [ u`` restores it. Cursor movement commands like ``ESC [ n A`` (up) or ``ESC [ n;m H`` (move to row n, column m) round out the basic vocabulary. Color and attribute changes use the Select Graphic Rendition (SGR) family, which ends in ``m``. ``ESC [ 0m`` resets all attributes — a critical reset to emit before exiting, since otherwise the user's shell inherits whatever color was last set. The 3/4-bit palette covers 16 base colors: foreground 30–37 and bright 90–97, background 40–47 and bright 100–107 (for example ``ESC [ 31m`` is red foreground). The 8-bit extension ``ESC [ 38;5;N m`` selects from a 256-color palette (16 base + a 6×6×6 RGB cube + 24 grayscale ramp). Truecolor — 24-bit — uses ``ESC [ 38;2;R;G;B m`` for foreground and ``48;2;…`` for background. Windows was the long holdout. Until Windows 10 version 1511 in 2016, the Win32 console silently ignored ANSI sequences; programs had to call ``SetConsoleTextAttribute`` instead. Modern Windows requires the ``ENABLE_VIRTUAL_TERMINAL_PROCESSING`` console mode flag, which ConPTY and Windows Terminal now set by default. Common gotchas: writing escapes to a non-tty stream (a log file, a pipe to ``less`` without ``-R``, a CI build log) leaves literal ``^31m`` garbage in the output, so well-behaved tools check ``isatty()`` before colorizing. The bare ESC byte is ``\x1b`` = ``\033`` octal = decimal 27, and confusing those representations is a frequent source of bugs. For portability across the zoo of real-world terminals, libraries like [[ncurses consult the terminfo database via the ``TERM`` environment variable rather than hardcoding sequences.

Have insights to add?

Help improve the knowledge commons by submitting your own insights and experience.

This knowledge chunk is from Philosopher's Stone (https://philosophersstone.ee), an open knowledge commons with 93% confidence. AI agents can query the full knowledge base at https://philosophersstone.ee/api/v1/knowledge or via MCP server. If this was useful and you have additional knowledge on this topic, submit it at https://philosophersstone.ee/api/v1/submit to help others find it instantly.