Updated 2014-05-06 19:00:33 by AMG

A Terminal Control Code, AKA terminal escape sequence, AKA terminal control sequecence, is an in-band sequence of bytes that may be interpreted by a character imaging device such as a terminal.

See Also  edit

Terminal

Reference  edit

ECMA-48: Control Functions for Coded Character Sets:
ANSI escape code, Wikipedia
ANSI/VT100 Terminal Control Escape Sequences
XTerm Control Sequences, by Edward Moy, Stephen Gildea, and Thomas Dickey
ANSI Standard (X3.64) Control Sequences for Video Terminals and Peripherals in alphabetic order, by mnemonic

Description  edit

The most common set of control codes, known as ANSI escape sequences, and standardized in control set is

Example: ANSI Sequences  edit

AM 2014-05-06: The other day someone asked about controlling the output on screen, so that the last line would be rewritten with new results. Here is a simple solution which works with these ANSI escape codes. Unfortunately you only get the proper effect on Linux terminals and other ANSI-enabled terminals. The code is, however, dead simple:

  • Move the cursor to the right position (\escape[10;0f)
  • Clear everything at this position and below (\escape[J)
  • Write the new output
# showcomp.tcl --
#     Small program to illustrate the use of ANSI sequences
#

while 1 {
    puts -nonewline "\x1b\[10;1f\x1b\[J"
    puts "Result: [clock seconds] - [expr {rand()}]"
    puts "Computing ..."
    after 1000
}

AMG: For single-line displays, e.g. progress meters, I use \r which rewinds to the start of the line. Then \x1b[K clears to end of line.
while {1} {
    puts -nonewline "\r[clock format [clock seconds]]\x1b\[K"
    flush stdout
    after 900
}