Rob van der Woude's Scripting Pages

ANSI sequences

This page discusses the use of ANSI.SYS in COMMAND.COM, both native in MS-DOS.

Unfortunately, ANSI.SYS is no longer available in CMD.EXE (Windows NT 4 and later).

NT's COLOR command can only be used to set the text and background color for the entire console, other ANSI.SYS features require third party utilities in CMD.EXE.

Use ANSI sequences to:

You can use ANSI sequences to creaty fancy prompts or text screens or even text mode animated screens (though it may take several days to create one ANSI animated screen).

For ANSI sequences to be shown, they have to be sent to the screen. You may use either TYPE, ECHO or a combination of PROMPT and ECHO OFF, or even any combination of these methods within a single batch file.

The only requirement to use ANSI sequences is the line:
in your CONFIG.SYS, at least for DOS systems.
Or use some other ANSI "interpreter", like ANSI.COM, by Michael J. Mefford.

OS/2 command prompts do not need ANSI.SYS, since the ANSI functions are built in the command processor. To use ANSI sequences on DOS command prompts in OS/2 you'll still need to load ANSI.SYS in CONFIG.SYS.
It is possible to disable ANSI in OS/2 with the command ANSI OFF
Likewise you may enable it again with ANSI ON

Windows NT doesn't support ANSI by default.
Check out George Roettger's home page for help on running ANSI.SYS in NT (plus many more NT tips).

Replace <Esc> with ASCII character 27 (see note) in text on screen (e.g. TYPEd files or ECHOed strings) or with $E in PROMPT strings.
Most DOS based editors will display the Escape character as a left arrow.
Most Windows based editors will display the escape character as a square.

Note: Inserting ASCII Character 27 (Esc):

• In EDIT, use Ctrl+P follwed by Alt+27 (press Alt, keep it pressed, and then type 27 on the numeric key pad).
• In Notepad or Wordpad, copy and paste the Escape character from some reference file you can create using EDIT.
• In Norton Commander or File Commander's editor, use Ctrl+Q followed by Alt+27 on the numeric key pad.
• In E and EPM (OS/2) just type Alt+27 on the numeric key pad.
• In UltraEdit, press Ctrl+I followed by the Esc key.

You may have to change the codepage to 437 in some editors.

You may combine as many text attributes as you see fit.

<Esc>[5m will give different results in different situations:
In plain DOS and in full screen OS/2 text mode it will result in blinking text.
In OS/2 and DOS windows it will result in a bright background.

For PROMPT strings some extra ANSI sequeunces are available.


Text color:

# Color Normal
0 Black <Esc>[30m <Esc>[1;30m <Esc>[40m
1 Red <Esc>[31m <Esc>[1;31m <Esc>[41m
2 Green <Esc>[32m <Esc>[1;32m <Esc>[42m
3 Yellow <Esc>[33m <Esc>[1;33m <Esc>[43m
4 Blue <Esc>[34m <Esc>[1;34m <Esc>[44m
5 Magenta <Esc>[35m <Esc>[1;35m <Esc>[45m
6 Cyan <Esc>[36m <Esc>[1;36m <Esc>[46m
7 White <Esc>[37m <Esc>[1;37m <Esc>[47m


Notes: (1) Use the COLOR command or CMD /T to set the default text colors in NT command line sessions
  (2) Learn how to change the looks of your command prompt by reading Microsoft's article: "HOW TO: Set the Command Processor Appearance in Windows XP".
Don't be fooled by the title, this works in older Windows versions too.

Back to the top of this page

Other text attributes:

<Esc>[0m Reset all previous text attributes
<Esc>[1m Bold or bright text
<Esc>[2m Bold off (not reliable; use <Esc>[0m instead)
<Esc>[4m Underlined (monochrome) or blue
<Esc>[5m Blinking text or bright background
<Esc>[7m Reversed text  
<Esc>[8m Invisible text (Invisible text)


Notes: (1) DOS will display blinking text with <Esc>[5m, OS/2 windows will give a bright background, and OS/2 Full screen will start with a bright background but will switch to blinking text as soon as it loses focus.
  (2) Text made invisible on screen with <Esc>[8m will still be visible on screen prints.
It is therefore not recommended for hiding passwords etc., unless you use cursor positioning and then <Esc>[K to wipe the password from the screen completely, immediately after inputting it.

Back to the top of this page

Cursor positioning:

ANSI Sequence Effect
<Esc>[r;cH Position cursor at row r and column c
<Esc>[nA Move cursor n rows up
<Esc>[nB Move cursor n rows down
<Esc>[nC Move cursor n columns forward (right)
<Esc>[nD Move cursor n columns back (left)
<Esc>[6n Show current cursor position
<Esc>[s Save current cursor position
<Esc>[u Restore previously stored cursor position

Back to the top of this page

Clear text area:

ANSI Sequence Effect
<Esc>[2J Clear screen
<Esc>[K Clear to end of line

Back to the top of this page

Set video mode:

ANSI sequence Video mode     ANSI sequence Video mode
40x25 B/W text   <Esc>[=7l disable wrap
40x25 color text   <Esc>[=13h
320x200 16 colors
80x25 B/W text   <Esc>[=14h
640x200 16 colors
80x25 color text   <Esc>[=15h
640x350 2 colors
320x200 4 colors   <Esc>[=16h
640x350 16 colors
320x200 2 colors   <Esc>[=17h
640x480 2 colors
640x200 2 colors   <Esc>[=18h
640x480 16 colors
<Esc>[=7h enable wrap   <Esc>[=19h
320x200 256 colors
Note: The l in <Esc>[=nl is a lower case L

Back to the top of this page

Keyboard macro's:

General usage:

inkey specifies the macro's hotkey
outstring specifies the keystrokes to be performed


65 81 <Esc>[65;81p Replace "A" (ASCII 65) with "Q" (ASCII 81)
(AZERTY to QWERTY translation the hard way?)
0;68 DIR;13 <Esc>[0;68;DIR;13p Pressing F10 (extended ASCII 0;68) will result in "DIR" being typed, followed by <Enter> (ASCII 13)


Notes: (1) The keyboard redirection or keyboard macro will be active only when ANSI is active: in command prompt sessions and batch files.
It cannot be used within application programs.
  (2) The keyboard redirection or keyboard macro is not permanent. You should re-execute the batch file that activates the macro every time you start a new command line session.

Back to the top of this page


Take a look at ANSIDEMO.BAT for some examples of cursor positioning and text attributes.

Shown below is a picture of the ANSI screen output for AN_APPLE.ANS, a fine example of ANSI art by Chris Blanton:

Screendump of AN_APPLE.ANS

View it in text mode by changing to the right directory and using the following command:

Click to download source

Back to the top of this page

ANSI.SYS Alternatives

There may be occasions where you don't know in advance if ANSI.SYS will be loaded or not.
You can check if ANSI.SYS was loaded using MEM /C but that only looks for ANSI.SYS or ANSI.COM.
Utilities like BATCHMAN do a better job since they search for functionality, not for a particular driver. (On the other hand, once you are using BATCHMAN, why not use that to display colored text etcetera? Unlike ANSI.SYS, BATCHMAN will even work in Windows 2000.)

In many cases ANSI.COM may be a better solution.
Like BATCHMAN, ANSI.COM was written by Michael J. Mefford.
When called - at the start of a (DOS) batch file - it will load only if no other ANSI "interpreter" is active. When loaded it will function just like ANSI.SYS would. At the end of the batch file, you may unload it again, freeing memory for other programs.
By using ANSI.COM, you can make sure that your batch files will display their fancy colors just the way you intended them to.

Unfortunately, ANSI.SYS is no longer available in CMD.EXE (NT 4 and later).
Using ANSI like features will require third party utilities.

Whereas ANSI.SYS could be used to set the color for a selection of the console text, NT's COLOR command will set the colors of the entire console.
So I wrote KOLOR.EXE to get or set the colors for a selection of text. It can be used in 32-bit and 64-bit environments.

Carlos M. wrote BG. Initially intended for Batch Games, it can be used to colorize console windows of any type.
It is a modern replacement for BATCHMAN, and unlike BATCHMAN it will even work in 64-bit environments.

When you want ANSI functionality for text "enhancements", have a look at Norman De Forest's EKKO, an ECHO enhancement that can process almost every PROMPT function including coloured text (even without ANSI driver).
And the best part is, it works just as well in Windows NT.

Back to the top of this page

page last uploaded: 2017-05-22, 07:27