Home Page
        Orders     Downloads     Support     Contact     Deutsch

Terminal Escape Sequences

Escape sequences are special codes that control the behavior of character-based terminals. This article shows examples of commonly used sequences in well known terminal types.
 

Table of Contents

 

Escape-Sequences and Terminals

In the past (before the PC), a VT100/VT102 terminal was to be a device consisting of a physical screen and keyboard. The terminal was connected to a central server and provided a user with the means to enter data into the server and see results that the server sent back to the user's screen. Thus the users had (rather inexpensive) terminals on their desk to provide them with shared access to a (prohibitively expensive) central computer.

Initially these terminals were basically teletype machines, which could print out text only from left to right and top to bottom. However, as the technology evolved, a method was introduced to embed control codes into the data sent to the terminal, in order to tell the terminal to format the text or perform other functions that affected the text output.

In many terminal types, in order to do this, the Escape or ESC character was used. ESC is a special control character in the ASCII character set which has the code 27 or hex 1B. When this character appears in the data that is sent to the terminal, it tells the terminal that the next few bytes are control information or a control-sequence, rather than text that is to be printed. Hence the term Escape Sequence.


Escape-Sequences Explained with the Example of a VT100 Terminal

The VT100 terminal made by DEC was a very successful terminal and its specific form of escape sequences, which is based on the ANSI X3.64 (ECMA-48) Standard, was carried over to other terminal types and are of relevance to this day, e.g. with the terminal type xterm which is prevalent in the Linux world.

As with all terminal types, VT100 is a terminal standard that allows the server to send text to the user's screen. Rather than just displaying text from left to right and top to bottom, by embedding special controls in the text, the application is given control over the terminal's placement and display characteristics of the text (location, color, etc.). These sequences begin with the ESC character, which is a byte with value 27 or 1B (hex) and depending on the context it is usally displayed either as \e or ^[ or as ESC.

For example, in order to send the text "This is an error!" to the user's screen with the word "error" underlined, the host would send  This is an esc[4m erroresc[0m!  to the terminal. Rather than displaying all the text, the VT100 terminal will interpret esc[4m  and esc[0m  as commands that tell it to highlight the text that is received between them, so the actual output will be:
This is an error!

Since Linux consoles are based on the xterm terminal type, which itself is based on the VT420, which again is derived from the initial VT100, you can do the above by issuing the following command:
echo -e "This is an \e[4m error \e[0m!"


ANSI/VT100 Control Codes and Esc-Sequences

Below, is a list of basic escape sequences for the VT100 terminal. DEC decided to start most of their sequences with an esc, followed by a square bracket [ and then text of variable length up to the next uppercase or lowercase letter.

Sequence Description Example
esc [ n A Cursor Up n times echo -e "\e[5A"
esc [ n B Cursor Down n times
esc [ n C Cursor Forward n times
esc [ n D Cursor Backward n times
esc [ row ; col H Cursor Position [row;column] echo -e "\e[24;1H"
esc [ n J Erase in Display:
n= 0/1/2 → below/above/all
echo -e "\e[2J"
esc [ n K Erase in Line
n= 0/1/2 → left/right/all
echo -e "\e[1K"
esc [ n ; n ... m Set text attributes
n= 1 → highlight; n=5→blink,
n=7→inverse, ...
echo -e "\e[5;7minverse&blinking\e[0m!"
esc [ n @ Insert n (Blank) Character(s)
esc 7 Save cursor position
esc 8 Jump to saved cursor position
esc [ ? 3 h Sets display to 132 columns per line
esc [ ? 3 l Sets display to 80 columns per line

Note: ESC equals ASCII code hex 1B or decimal 27, other italic values like n or row are decimal numbers.

Attribute Description Example
0 Reset to Default
1 Bold/Intense
4 Underline
5 Blinking echo -e "\e[5mblinking\e[0m"
7 Inverse
31-37 Foreground Color (black, red, green, yellow, blue, magenta, cyan, gray) echo -e "\e[31mRed\e[0m"
41-47 Background Color (black, red, green, yellow, blue, magenta, cyan, gray)

A fulll list of terminal emulation sequences for VT100 can be found in the VT100 User Guide on vt100.net.


Wyse Control Codes and Esc-Sequences

Wyse was another influential manufacturers of terminals. Wyse also used the esc character to embed control codes in the terminal data, but they used a form for their escape sequences that was different from VT100. In their format, the character (or letter) after the esc determines what the sequence is and what will follow, e.g. the esca and esc= sequences both move the cursor to a given row and column, but in one case, what follows are decimal numbers for row and column, while in the other case two more characters follow, which are then decoded based on a table.

Sequence Description Example
esc j Cursor Up echo -e "\ej"
esc a rr R cc C Move cursor to row and column
rr and cc decimal values
echo -e "\ea4R12C"
esc = r c Move cursor to row and column
r and c according to Row/Column Codes
echo -e "\e=0AC"
esc Y Clear end of page to space echo -e "\eY"
esc + Clear screen to spaces echo -e "\e+"
esc G n Set text attributes
n= according to Attribute Codes
echo -e "\eG4"

Note: ESC equals ASCII code hex 1B or decimal 27, other italic values like n, r or c are codes based on the Wyse Reference Manual.

A more complete list of emulation sequences for Wyse can be found in the WY 50 Quick Reference Manual.


Testing a VT100 via 'vttest'

Since the 90's terminal are mostly software that emulates the functions of the original terminals. Since the escape sequences and their interworkings and side-effects when used in unexpected combinations are quite intricate, the quality of a terminal emulator depends on how faithfully it mimicks the original behavior.

The quality of a terminal emulator can be measured by testing how fully it implements the terminal emulation sequences (see above) and how closely its matches the original terminal's behavior in borderline situations (i.e. when it receives unexpected, faulty or weird combinations of sequences).

Vttest is an application that is used to demonstrate features of VT100 and related terminals, or emulations thereof, such as xterm. The program was originally written in 1986 by Per Lindberg.

 

 

 

 

 

 

 

Downloads
Orders
Contact
Support
Terms of Use
Privacy Policy
pixel