Ansi Escape Sequences

To obtain local full screen and keyboard control of a remotely connected terminal or computer in telecommunications a control character oriented keyboard and screen control system is necessary. This tutorial describes how the ANSI escape sequences are used in a terminal. An ANSI escape sequence is a series of characters, beginning with the Escape character, used to define functions to the terminal. You can use these escape sequences to affect cursor movement, erase information from the screen display, set the graphics mode, and reassign keys.

There are several ways to execute ANSI escape sequences:

  • You can enter them at the command line using the PROMPT.
  • You can create them in a file and then issue them with the TYPE command.
  • You can include escape sequences in your programs.
Ansi Escape Sequence Syntax

Each ANSI escape sequence has the same basic syntax . ESC[parameters-command (spaces added for readability). ESC is the 1 byte code for escape. The escape code is $e at the command line using PROMPT.

Ansi Cursor Control Functions
  • ESC[#;#H - Cursor Position (CUP)

The first # specifies the line number, the second # specifies the column. The default for both is 1

  • ESC[#A - Cursor Up (CUU)

Moves the cursor UP # number of lines

  • ESC[#B - Cursor Down (CUD)

Moves the cursor DOWN # number of lines

  • ESC[#C - Cursor Forward (CUF)

Moves the cursor RIGHT # number of columns

  • ESC[#D - Cursor Backward (CUB)

Moves the cursor LEFT # number of columns

  • ESC[#;#f - Horizontal & Vertical Position

(same as ESC[#;#H)

  • ESC[s - Save Cursor Position (SCP)

The current cursor position is saved. The cursor position can be restored using (RCP) ESC[u.

  • ESC[u - Restore cursor position (RCP)

Restores the cursor position saved with the (SCP) sequence ESC[s.

  • ESC[2J - Erase Display (ED)

Clears the screen and moves to the home position

  • ESC[K - Erase Line (EL)

Clears the current line from the cursor position

  • ESC[#;…;#m - Set Graphics Rendition (SGR)

(optional text may be included in the …) . The Escape parameters for (SGR)

  • 0 All attributes OFF
  • 1 Bold ON
  • 4 Underscore ON (monochrome only)
  • 5 Blink ON
  • 7 Reverse Video ON
  • 8 Concealed ON
  • 30 Black Foreground
  • 31 Red
  • 32 Green
  • 33 Yellow
  • 34 Blue
  • 35 Magenta
  • 36 Cyan
  • 37 White
  • 40 Black Background
  • 41 Red
  • 42 Green
  • 43 Yellow
  • 44 Blue
  • 45 Magenta
  • 46 Cyan
  • 47 White
  • ESC[=#h Set Mode (SM)

Escape parameters for Set Mode (SM)

  • 0 40×25 black& white
  • 1 40×25 color
  • 2 80×25 black & white
  • 3 80×25 color
  • 4 320×200 color
  • 5 320×200 black & white
  • 6 640×200 black & white
  • 7 wrap at end of line
  • ESC[=#I Reset Mode (RM)

Uses same parameters as Set Mode (SM)

  • ESC[=#;#…#p or ESC[string–Keyboard Reasignment

The first # is the ASCII code for the key being changed, the second # or “string” is the replacement, with one exception - if the first # is a NULL then the first and second # represent the key being reassigned. Examples

  • PROMPT $e[7m$n$p$e[m creates a standard prompt $n$g except in reverse video.
  • PROMPT $p$g$e[33;44m - on a color video, this will set the prompt to display the directory $p$g and display it in red against blue.
  • ESC[65;81p - reassigns A to become Q
  • ESC[0;68;“DIR”;13p - reassigns the F10 key to DIR + [CR]
  • ESC[2J - clear the screen & home cursor
  • ESC[7m - all subsequent characters in reverse video
  • ESC[7;5m - all subsequent characters reverse and blinking
  • ESC[13;40f - move cursor to center of screen
ASCII Code-- Extended Key
  • 16 - 25 ALT Q,W,E,R,T,Y,U,I,O,P
  • 30 - 38 ALT A,S,D,F,G,H,J,K,L
  • 44 - 40 ALT Z,X,C,V,B,N,M
  • 59 - 68 [F1] through [F10]
  • 84 - 93 SHIFT[F1] through SHIFT[F10]
  • 94 - 103 [F1] through [F10]
  • 104 - 113 ALT[F1] through ALT[F10]

Using the above ASCII Code to Extended Key conversion table and the redefine key syntax:

  • prompt $e[0;##;“command”p {without trailing ENTER}
  • prompt $e[0;##;“command”;13p {with trailing ENTER}

where ## = the ASCII code of the key to redefine and command = the command to assign to the Extended Key

We use cookies to personalize and enhance your experience on our site. By using our site, you agree to our use of cookies.
  More information about cookies