An escape sequence is a series of characters used to change the state of computers and their attached peripheral devices. These are also known as control sequences, reflecting their use in device control. Some control sequences are special characters which always have the same meaning. Escape sequences use an escape character to change the meaning of the characters which follow it. The characters can be interpreted as a command to be executed rather than data.
Escape sequences are commonly used when the computer and the peripheral have only a single channel in which to send information back and forth. If the device in question is "dumb" and can only do one thing with the information being sent to it (for instance, print it) then there is no need for an escape sequence. However most devices have more than one capability, and thus need some way to distinguish information that is to be treated as data from information that is to be treated as commands.
An escape character is usually assigned to the Esc key on a computer keyboard, and can be sent in other ways than as part of an escape sequence. For example, the Esc key may be used as an input character in editors such as EMACS, or for backing up one level in a menu in some applications. The Hewlett Packard HP 2640 terminals had a key for a "display functions" mode which would display graphics for all control characters, including Esc, to aid in debugging applications.
If the Esc key and other keys that send escape sequences are both supposed to be meaningful to an application, an ambiguity arises, if a terminal or terminal emulator is in use. In particular, when the application receives the ASCII escape character, it is not clear whether that character is the result of the user pressing the Esc key or whether it is the initial character of an escape sequence (e.g., resulting from an arrow key press). The traditional method of resolving the ambiguity is to observe whether or not another character quickly follows the escape character. If not, it is assumed not to be part of an escape sequence. Obviously this heuristic can fail under some circumstances, but in practice it works reasonably well, especially with faster modern communication speeds.
Escape sequences date back at least to the 1874 Baudot code.
The Hayes command set, for instance, defines a single escape sequence, +++. (In order not to interpret +++ which may be a part of data as the escape sequence the sender stops communication for one second before and after the +++) .When the modem encounters this in a stream of data, it switches from its normal mode of operation which simply sends any characters to the phone, to a command mode in which the following data is assumed to be a part of the command language. You can switch back to the online mode by sending the O command.
The Hayes command set is modal, switching from command mode to online mode. This is not appropriate in the case where the commands and data will switch back and forth rapidly. An example of a non-modal escape sequence control language is the VT100, which used a series of commands prefixed by the Control Sequence Introducer, escape-[.
Full article ▸