Programming


ANSI Escape Sequences (for ANSI.SYS)


Cursor Commands
Cursor Up {ESC}[<row>A
Moves the cursor up the specified number of rows without changing the column.

<row> is a number from 1 through 24 that specifies how many rows the cursor is to be moved up.
If you omit <row>, DOS moves the cursor up one row.
examples {ESC}[13A Move the cursor up 13 rows

{ESC}[A Move the cursor up 1 row

Cursor Down {ESC}[<row>B
Moves the cursor down the specified number of rows without changing the column.

<row> is a number from 1 through 24 that specifies how many rows the cursor is to be moved down.
If you omit <row>, DOS moves the cursor down one row.
examples {ESC}[8B Move the cursor down eight rows.

{ESC}[B Move the cursor down one row.

Cursor Right {ESC}[<col>C
Moves the cursor right the specified number of columns without changing the row.

<col> is a number from 1 through 79 that specifies how many columns that cursor is to be moved right.
If you omit <col>, DOS moves the cursor right one column.
examples {ESC}[40C Move the cursor right 40 columns.

{ESC}[C Move the cursor right one column.

Cursor Left {ESC}[<col>D
Moves the cursor left the specified number of columns without changing the row.

<col> is a number from 1 through 79 that specifies how many columns the cursor is to be moved left.
If you omit <col>, DOS moves the cursor left one column.
examples {ESC}10[D Move the cursor left ten columns.

{ESC}[D Move the cursor left one column.

Move Cursor {ESC}[<row>;<col>H or {ESC}[<row>;<col>f
Moves the cursor to the specified row and column.

<row> is a number from 1 through 25 that specifies the row to which the cursor is to be moved. If you omit <row>, DOS moves the cursor to row 1. To omit <row> but specify <col>, enter the semicolon to show the <row> is omitted.
<col> is a number from 1 through 80 that specifies the column to which the cursor is to be moved. If you omit <col>, DOS moves the cursor to column 1.
If you omit both <row> and <col>, DOS moves the cursor to the home position (row 1, column 1--the upper left corner of the screen).
examples {ESC}[;10H Move the cursor to column 10, row 1.

{ESC}[H Move the cursor to row 1, column 1.

Save Cursor Position {ESC}[s
Stores the current row and column position of the cursor.

You can move the cursor to this location with a Restore Cursor Position command.
examples {ESC}[s Save the current cursor position.

Report Cursor Position {ESC}[6n
Returns the current row and column position of the cursor in the form {ESC}[<row>;<col>R.

<row> is a number from 1 through 25 that specifies the row where the cursor is located.
<col> is a number from 1 through 80 that specifies the column where the cursor is located.
examples {ESC}[6n Report the current cursor position.

Restore Cursor Position {ESC}[u
Moves the cursor to the row and column position most recently saved with a Save Cursor Position command.
examples {ESC}[u Move the cursor the row and column last saved with a Save Cursor Position command.

Erase Commands
Erase Display {ESC}[2J
Erases the entire display (equivalent to the DOS Clear Screen or cls command).
examples {ESC}[2J Erase the screen.

Erase to End of Line {ESC}[K
Erases from the current cursor position through the end of the line that contains the cursor.
examples {ESC}[K Erase from the cursor to the end of the line.

Display Attribute and Mode Commands
Set Attribute {ESC}[<attr>m
Turns on a characteristic or attribute of the display, such as high intensity, blink, or foreground and background color.

<attr> specifies the display attribute to be turned on. More than one attribute can be specified by using a semicolon to separate the attribute numbers. <attr> can be any of the following:
AttributeColor(Foreground)(Background)
None0Black3040
High Intensity1Red3141
Underline4Green3242
Blink5Yellow3343
Reverse7Blue3444
Invisible8Magenta3545


Cyan3646


White3747
If you omit <attr>, all attributes are turned off (equivalent to specifying <attr> as 0).
examples {ESC}[1m High intensity.

{ESC}[1;5m High intensity and blink.

{ESC}[30;46m Black foreground, cyan background.

{ESC}[m Turn off all attributes.

{ESC}[0m Turn off all attributes.

{ESC}[0;1;36m Turn off all attributes, then turn on high-intensity cyan foreground.

Set Display Mode {ESC}[=<mode>h
Sets the width and color capability of the display (generally equivalent to the DOS MODE command). This command can also be used to cause lines longer than 80 characters to be broken at the 80th character and continued on the next line, rather than truncated at the 80th column; this is called line wrap. It can be turned off with the Turn Off Line Wrap command. Note the equal sign (=) that precedes <mode>.

<mode> specifies the display mode. It can be one of the following:
ValueDisplay Mode
040 columns by 25 rows, black and white
140 columns by 25 rows, color on
280 columns by 25 rows, black and white
380 columns by 25 rows, color on
4320 by 200 graphics, color on
5320 by 200 graphics, black and white
6640 by 200 graphics, black and white
7Turn on line wrap
examples {ESC}[=1h Set the display to 40 by 25 color on.

{ESC}[=7h Continued lines longer than 80 characters, don't truncate them.

Turn Off Line Wrap {ESC}[=7l
Causes lines longer than 80 characters to be truncated at the 80th character, rather than continued to the next line.
examples {ESC}[=7l Truncate lines longer than 80 characters.

Keyboard Commands
Define Key {ESC}[<key code>;<key code>p
Assigns one or more characters to be produced when you press a key.

<key code> specifies the key to be defined. If the key is one of the standard ASCII characters, <key code> is a number from 1 through 127. If the key is a function key, keypad key, or a combination of the <Shift>, <Ctrl>, or <Alt> key and some other key, <key code> is two numbers separated by a semicolon, found in ANSI Key Code table.
<result> is the character or characters to be produced when a key is pressed. It can be specified as an ASCII code, an ANSI key code, a string enclosed in quotation marks, or any combination of codes and strings separated by semicolons.
To restore a key to its original meaning, enter a Define Key command that sets <result> equal to <key code>.
examples {ESC}[126;92p Redefine the tilde <~> key as a backslash <\>.

{ESC}[126;126p Restore the tilde <~> key to its original meaning.

{ESC}[0;112;"dir|sort";13p Redefine <Alt-F9> as a Directory command piped to a Sort command, followed by a Carriage Return.

{ESC}[0;112;0;112p Restore <Alt-F9> to its original meaning.

Notes
Entering the {ESC} character In DOS:  Hold down the <Alt> key while typing 27 on the keyboard keypad.
In Windows:  Hold down the <Alt> while typing 0027 on the keypad.

Using ANSI Escape Sequences To use ANSI escape sequences, the file ANSI.SYS must be on your system disk and named as a device driver in the CONFIG.SYS file.

Place a line in CONFIG.SYS similar to this one:

     DEVICE=<path>\ANSI.SYS

where <path> is the path name of the directory where ANSI.SYS is stored.

SCREEN.BAT
(ZIPPED file, 2k)
This batch file demonstrates some of the Display Attribute commands.


[  Index  |  Technical Notes Page  ]

This page is for INFORMATIONAL PURPOSES ONLY.

Page author: Dawn Rorvik (rorvikd@evergreen.edu)
Last modified: 02/06/1999