Ansi Escape Sequences


Ansi.sys
Ansi Escape Sequences
Ansi Commands
Ansi Key Codes
Index

Once Ansi.sys has been loaded, it's various functions can be accessed at any time by sending one or more "Escape Sequences" to the screen. There are three ways to do this:

  1. Using the PROMPT command along with the "$E" special character.
    Although PROMPT can certainly be used from the command line to enter Escape Sequences on the fly, it is most often used in Autoexec.bat to set the screen colours along with the actual Dos prompt. It can also be used in batch files to construct fancy menus in different colours. The syntax is:

    PROMPT $E[Code

    Multiple sequences can be entered by simply stringing them together (ie. PROMPT will accept any number of parameters):

    PROMPT $E[Code1 $E[Code2 $E[Code3
    Note: Spaces between the sequences make them easier to decipher but are not required.

    If you are using the standard prompt, include $P$G in the PROMPT string or you will be left simply with a flashing cursor. If you are using a non-standard prompt, you might want to save this so that it can be restored when required:

    SET PT=%prompt%
    PROMPT $E[Code1 $E[Code2 $E[Code3 %PT%

    When screen colours, keyboard mappings, etc. are changed in this way, the effects stay in force until changed again (or the system is re-booted). Entering PROMPT $P$G will not restore the default colours/attributes.

  2. ECHO an Escape Sequence to the screen.

    The procedures for using ECHO depends on whether you are using a batch file or working directly from the command line. In a batch file, one or more ECHO statements can be compiled into a list using EDIT. The "Escape character" (ASCII 27) is entered by pressing Ctrl+P followed by the "ESC" key and displays as ASCII 27. The codes are set by running the batch file from the command line or calling it from another batch file (eg. Autoexec.bat).

    Using the ECHO method from the command line is (slightly) more complicated because the first character (ASCII 27 or "ESC") is interpreted as an instruction to "clear the command line" which doesn't get you very far. This is a real nuisance and seems to be the result of a conflict between two sets of conventions originating way back in history. Along with some other very useful notes, Bob Blackledge wrote:

    ANSI.SYS is MicroSoft's implementation of the ANSI X3.64 standard "Control Sequences for Video Terminals and Peripherals" (dating from 1976 or thereabouts, and also defined in ISO DP-6429, ECMA-35 and probably other standards organizations around the world like IEC).

    ANSI (and the standard) defining the use of the ESC character as part of a Control Sequence Introducer (CSI) to expand the scope of the originally defined 7-bit ASCII character set (later extended to the 8-bit character set we enjoy today) into multi-character sequences, existed before MicroSoft Corp really did. Microsoft's implementation is a subset of the standard, but well, pretty poor. Most terminals in existence even at that time implemented a more complete subset of the standard (such as DEC's classic VT100).

    Several standardized escape sequences are defined (and even more were created for proprietary usage and not standardized) but the sequence ESC followed by [ is the most often defined. Microsoft limited themselves to these commands exclusively.

    Anyway, the way round this is to create an environmental variable (say "ESC") comprising just the ASCII 27 character. This is done by creating a batch file (Esc.bat, say) in EDIT that has the one line:

    SET ESC=ASCII 27

    where ASCII 27 is entered by pressing Ctrl+P followed by the "ESC" key. After running Esc.bat, ASCII 27 can be entered on the command line by entering %ESC% in its place. For example, to assign the character "¼" to Alt-4 one could enter:

    ECHO %ESC%[0;123;172p

  3. Sending a text file containing a series of Escape Sequences to the screen.
    A file is created in EDIT that contains one or more Ansi.sys commands and this is copied to the screen using the TYPE filename or COPY filename CON commands. As mentioned before, the "Escape character" (ASCII 27) is entered by pressing Ctrl+P followed by the "ESC" key. To assign the character "½" to Alt-2 and "¼" to Alt-4 one could create a text file called "Fraction.ans" containing:

    ASCII 27[0;121;171p
    ASCII 27[0;123;172p

    This assignment would be carried out by entering:

    TYPE Fractions.ans

    either at the command line or from within a batch file.

Although I have said that the ASCII 27 character should be inserted using EDIT, any text editor that has provision for entering "reserved/special" characters can, of course, be used. However, EDIT is the only one that I know to have this facility - I don't think any of the standard Win95/8 utilities let you do this (eg. Notepad, Sysedit, or the System Configuration Utility). If I hear of others, I will add them to the following table.

Text EditorTo enter the "ESC" characterDisplays as:
EDITCtrl-P followed by the "ESC" keyleft arrow


Thanks to Bob Blackledge for his information on the "Environmental Variable" technique for entering the ASCII 27 character.
This page last revised:
November 11, 2000.