ANSI Escape Code
The in-band byte sequences, starting with the escape character, that tell a terminal to move the cursor, change color, or clear the screen, inherited from 1970s video terminals and still how every command-line tool draws.
An ANSI escape code is a sequence of bytes, beginning with the escape character, that a terminal interprets as a command instead of text to print: move the cursor, change the color, clear the line. Every colored prompt, progress bar, and full-screen command-line program draws itself this way.
They date from the video terminals of the 1970s. Each manufacturer had its own control codes, so software had to be written for specific models. ECMA-48, first published in 1976 and adopted by ANSI as X3.64 in 1979, defined a common set, and DEC's VT100 terminal of 1978 implemented it so successfully that "VT100-compatible" became the baseline every terminal emulator still targets; see Pseudoterminal and Path Dependence.
The format: the escape byte (27), an opening bracket, optional numeric parameters separated by semicolons, and a final letter naming the command. Common ones:
- Colors and styles, ending in
m:ESC[1mfor bold,ESC[31mfor red text,ESC[0mto reset. The original eight colors grew to 256, then to full 24-bit color in modern terminals. - Cursor movement, such as up, down, or to an exact row and column.
- Erasing part or all of a line or the screen.
- Alternate screen, which full-screen programs switch to so the shell's history reappears intact when they exit.
The codes are in-band: commands travel in the same stream as the text. That makes them simple, since any program that can print can draw, but it has costs:
- Output redirected to a file fills with stray codes, so well-behaved tools check whether they're writing to a terminal before using color, and honor the
NO_COLORconvention; see Environment Variable. - Untrusted text is dangerous to print. A log line or filename containing escape codes can hide text, rewrite the title bar, or on some old terminals type commands back into the input. It's the terminal's version of Cross-Site Scripting, and the fix is the same: escape untrusted output before displaying it.
- Capabilities vary. Programs once consulted a database, terminfo, to learn what each terminal supported. Most now assume a modern emulator.
Full-screen programs such as editors and system monitors are built entirely from these codes, redrawing only changed cells to keep output small; see Terminal User Interface and Virtual DOM. Newer extensions keep arriving through the same channel: clickable links, inline images, and clipboard access, each a negotiated extension to a 1970s protocol; see Backward Compatibility.
Text encoding interacts with all of this. A terminal must know how many columns each character occupies, and wide characters such as emoji and CJK ideographs break layouts when program and terminal disagree; see Character Encoding.
See also3
Clipboard
The shared buffer behind cut, copy, and paste, invented at Xerox PARC, which offers data in several formats at once and has become a channel for snooping and for commands that paste differently from how they looked.
Systems & Tooling14 connections
Serial Port
The old, simple connection that sends data one bit at a time, inherited from teleprinters and modems, gone from consumer computers but still the last-resort console for servers, network gear, and embedded boards.
Systems & Tooling10 connections
Terminal Multiplexer
A program that runs many terminal sessions inside one window, splits it into panes, and keeps the sessions alive on a server after the connection drops, so long jobs and remote work survive disconnection.
Systems & Tooling10 connections
Linked from7
- Character EncodingSystems & Tooling
The mapping between characters and bytes, and the source of a specific family of invisible bugs.
- ClipboardSystems & Tooling
The shared buffer behind cut, copy, and paste, invented at Xerox PARC, which offers data in several formats at once and has become a channel for snooping and for commands that paste differently from how they looked.
- Cross-Site ScriptingWeb Platform
Injecting attacker-controlled script into a page that a victim's browser then executes with the victim's own privileges.
- Line EndingSystems & Tooling
The invisible characters marking where a line of text ends, split three ways since the teletype era between Unix, Windows, and classic Mac OS, and still breaking scripts and diffs.
- Serial PortSystems & Tooling
The old, simple connection that sends data one bit at a time, inherited from teleprinters and modems, gone from consumer computers but still the last-resort console for servers, network gear, and embedded boards.
- Terminal MultiplexerSystems & Tooling
A program that runs many terminal sessions inside one window, splits it into panes, and keeps the sessions alive on a server after the connection drops, so long jobs and remote work survive disconnection.
- Terminal User InterfaceGraphics & Rendering
An interface rendered as text in a terminal, and the design conventions that make command-line tools humane.