ANSI/VT100 Terminal Control Escape Sequences

When writing BASH or Perl scripts I find it dramatically improves its effectiveness and ease-of-use to employ colour and formatting controls. This is done through the use of ANSI Escape Codes.

The following is a partial listing of the VT100 control set. The ASCII “escape” character (decimal 027, hex 0x1B) is shown as \e since this is the code used in BASH and Perl. Bracketed tags are modifiable decimal parameters, e.g., {ROW} is replaced by a row number.

In Perl you just include “\e” in a variable or printed output, e.g.,:

print "\e[2J\ec";     #|Clear the screen, go Home, reset all settings

In BASH the technique is very similar:

Yellow=$"\e[01;33m"
Reset=$"\e[0m"

echo -n "This is ${Yellow}very${Reset} important!"

History

Some time in the 1960s or 1970s the Digital Equipment Corporation has a fabulously popular terminal called the VT100. This terminal had certain control sequences that would cause it to do things never done by display terminals until then. When a colour terminal was introduced the codes were expanded to include colour controls. These codes became a de facto standard that other terminals as well as the IBM PC (and subsequent clones) employed.

The VT100 codes have been expanded for PCs. I saw one site that called the codes “ANSIPlus”, but whether this is an official name or not I do not know. In any case, the codes below should work on all modern equipment.

Non-visual Codes (reset &c.)

Device Status
Query Device Code \e[c Requests a Report Device Code response from the device.
Report Device Code \e[{code}0c Generated by the device in response to Query Device Code request.
Query Device Status \e[5n Requests a Report Device Status response from the device.
Report Device OK \e[0n Generated by the device in response to a Query Device Status request; indicates that device is functioning correctly.
Report Device Failure \e[3n Generated by the device in response to a Query Device Status request; indicates that device is functioning improperly.
Query Cursor Position \e[6n Requests a Report Cursor Position response from the device.
Report Cursor Position\e[{ROW};{COLUMN}R Generated by the device in response to a Query Cursor Position request; reports current cursor position.
Display Set-up
Reset Device\ec Reset all terminal settings to default. On my PC this causes the screen to clear and cursor to go to the home position, but I don't know that this would always be the case.
Enable Line Wrap\e[7h Text wraps to next line if longer than the length of the display area.
Disable Line Wrap\e[7lDisables line wrapping.
Fonts
Font Set G0\e(Set default font.
Font Set G1\e)Set alternate font.
Cursor Control
Cursor Home\e[{ROW};{COLUMN}HSets the cursor position. If no row/column parameters are provided (i.e., \eH) the cursor will move to the home position (the upper left of the screen).
Cursor Up\e[{COUNT}AMoves the cursor up by COUNT rows; the default count is 1.
Cursor Down\e[{COUNT}BMoves the cursor down by COUNT rows; the default count is 1.
Cursor Forward\e[{COUNT}CMoves the cursor forward by COUNT columns; the default count is 1.
Cursor Backward\e[{COUNT}DMoves the cursor backward by COUNT columns; the default count is 1.
Force Cursor Position\e[{ROW};{COLUMN}fIdentical to Cursor Home.
Save Cursor\e[sSave current cursor position.
Unsave Cursor\e[uRestores cursor position after a Save Cursor.
Save Cursor & Attrs\e7Save current cursor position.
Restore Cursor & Attrs\e8Restores cursor position after a Save Cursor.
Scrolling
Scroll Screen\e[rEnable scrolling for entire display.
Scroll Screen\e[{start};{end}rEnable scrolling from row {start} to row {end}.
Scroll Down\eDScroll display down one line.
Scroll Up\eMScroll display up one line.
Tab (Tabulator) Control
Set Tab\eHSets a tab at the current position.
Clear and Reset Tabs\e[#;#;#gFirst number is clear function, second and third are new settings. The primary functions are:
0=Clear current horizontal. No number is the same as zero: \e[g == \e[0g
1=Clear current vertical
2=Clear all
3=Clear all horizontal
4=Clear all vertical
\e[gClear horizontal tab at the cursor position.
\e[1gClear vertical tab at the cursor position.
\e[2;8;4gClear all tabs, then set horizontal tabs every 8 columns and vertical tabs every 4 lines.
\e[3gClear all horizontal tabs. NB: This does not reset all TABs to their original 8-character spacing; rather it eliminates all TABs, so if you issue a TAB character the cursor will position itself at the right-most edge of the screen.
\e[3;8gClear all horizontal tabs, then set new tabs every 8 spaces.
\e[4;4gClear all vertical tabs, then set vertical tabs every 4 lines.
Erasing Text
Erase to End of Line\e[KErases from the current cursor position to the end of the current line.
Erase to Start of Line\e[1KErases from the current cursor position to the start of the current line.
Erase Line\e[2KErases the entire current line.
Erase Down\e[JErases the screen from the current line down to the bottom of the screen.
Erase Up\e[1JErases the screen from the current line up to the top of the screen.
Erase Screen\e[2JErases the screen with the background colour and moves the cursor to home.
Printing (for terminals with a serial port that support local printing)
Print Screen\e[iPrint the current screen.
Print Line\e[1iPrint the current line.
Stop Print Log\e[4iDisable log.
Start Print Log\e[5iStart log; all received text is echoed to a printer.
Define Key
Set Key Definition\e[{key};“{string}“pAssociates a string of text to a keyboard key. {key} indicates the key by its ASCII value in decimal.

Display Attributes (colour codes)

To set a colour or other visual attribute (e.g., bold) use the code sequence \e[{attr1};…;{attrn}m. For example, if you want to set the font colour to bold yellow with a red background the code would be \e[1;33;41 or \e[41;33;01 (the sequence of the codes is not important, and you can zero-pad if you like).

CodeAttributeForeground coloursBackground colours
0 Reset all attributes 30 Black 40 Black
1 Bright 31 Red 41 Red
2 Dim 32 Green 42 Green
4 Underscore 33 Yellow 43 Yellow
5 Blink 34 Blue 44 Blue
7 Reverse 35 Magenta 45 Magenta
8 Hidden 36 Cyan 46 Cyan
37 White 47 White