Programming


ANSI.SYS Escape Sequences


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:
Text AttributeValue
None0
High Intensity
(bold)
1
Underline
(monochrome display only)
4
Blink5
Reverse7
Invisible8
 
Color
Attribute
Foreground
Value
Background
Value
Black3040
Red3141
Green3242
Yellow3343
Blue3444
Magenta3545
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:
Display ModeValue
40 columns by 25 rows, black and white0
40 columns by 25 rows, color on1
80 columns by 25 rows, black and white2
80 columns by 25 rows, color on3
320 by 200 graphics, color on4
320 by 200 graphics, black and white5
640 by 200 graphics, black and white6
Turn on line wrap7
    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>;<result>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 another key, <key code> is two numbers separated by a semicolon and can be found in the ANSI.SYS 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.SYS 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:  Press and hold the <Alt> key, then type 27 on the keypad.
  • In Windows:  Press and hold the <Alt> key, then type 0027 on the keypad.
  • Exceptions:  Sometimes the above keystrokes do not work. Try one of the following methods:
    • In MS-DOS EDITOR and QBASIC, type any one of these:
      • <Ctrl>P, <Alt>027
      • <Ctrl>P, <Ctrl>[
      • <Ctrl>P, <Esc>
    • In Microsoft WORD, use a macro:
      • Sub AsciiEscChar()
           ' Insert ASCII Esc character.
           Selection.TypeText Chr(027)
        End Sub
      • Save the file as "Text Only" or "MS-DOS Text".
    • In Microsoft NOTEPAD and WORDPAD:
      • Copy the {ESC} character from another text file and paste it into the document.

Enabling ANSI.SYS

Before using escape seqences, ANSI.SYS must be named as a device driver in the CONFIG system file.

For Windows 95, Windows 98 and DOS:
  • Create or edit the CONFIG.SYS file.   (Found in the root directory.)
  • Add the following line to the file:
    DEVICE=<path>\ANSI.SYS
    where <path> is the full path of the ANSI.SYS file.   (Usually found in the WINDOWS directory.)
  • Save CONFIG.SYS with the new line.
  • Check that a copy of ANSI.SYS exists in the specified path location.
  • Restart the computer to complete the change.
For Windows NT, Windows 2000 and Windows XP:
  • Create or edit the CONFIG.NT file.   (Usually found in the WINNT\SYSTEM32 directory.)
  • Add the following line to the file:
    DEVICE=%systemroot%\system32\ANSI.SYS
  • Save CONFIG.NT with the new line.
  • Check that a copy of ANSI.SYS exists in the specified path location.
  • Restart the computer to complete the change.
Restrictions:
  • Windows NT does not support ANSI.SYS escape sequences in Win32 Console applications.
  • The Windows 2000/NT Command Interpreter, CMD.EXE, does not support ANSI.SYS. Use COMMAND.COM instead.

Using ANSI.SYS Escape Sequences

Because ANSI.SYS commands control the console device, they must be typed at the keyboard or sent to the display.

  • Put the ANSI.SYS commands in a file and display the file with the TYPE or COPY command.
  • Use the PROMPT command with the command prompt code, $e.
    Example:
    prompt $e[1;37;44m   (Set the text color to bright white and the screen color to blue.)
  • Use the ECHO command in a batch file.
    Example:
    echo {ESC}[8;26H   (Move the cursor to row 8, column 26.)
    To execute a batch file containing ANSI commands in Windows 2000/NT, use one of the following methods:
    • Open a DOS command prompt window and type the <batch path>.
    • Type %SystemRoot%\system32\COMMAND.COM /c <batch path> at the Run command line.
    • Create a program information file (PIF) by making a shortcut of COMMAND.COM, then set the Cmd Line property to the <batch path> and the Advanced Program properties to %SystemRoot%\system32\AUTOEXEC.NT and %SystemRoot%\system32\CONFIG.NT.
  • Use the WRITE, PRINT or a similiar command in FORTRAN, C, BASIC, etc.
    Example:
    WRITE(*,*)'{ESC}[2J'   (Clear the screen.)

File Syntax and Parameters


ANSI.SYS Examples

Batch File

SCREEN.BAT demonstrates some Display Attribute and Cursor commands.

Command Prompt

Type the line below in a COMMAND.COM window to change the DOS command prompt.

  • prompt=$_$d$_$t$h$h$h$_$e[1;37;43mMy Computer$e[44m $p$g
  • View a screen shot.

Other Resources


[  Index  |  Technical Notes  ]

DISCLAIMER

Page author: Dawn Rorvik (rorvikd@evergreen.edu)
Last modified: 07/17/2003