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 not available in Windows NT4..8.1.

Windows NT4..8.1'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 Windows NT4..8.1's CMD.EXE.

In Windows 10, ANSI functionality is built in CMD.EXE by default, without requiring to load ANSI.SYS.

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:
DEVICE=ANSI.SYS
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.

 

Manually inserting ASCII Character 27 (Esc) in your text editor:

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

Insert special characters (including Esc) in batch files using the FORFILES command:

FORFILES interprets hexadecimal characters (0xdd format) in the command line "on-the-fly".
FORFILES will repeat the specified command for each file matching the optional file mask (default: *.*) in the optionally specified directory (default: current directory).
To make sure the command will be executed once, specify %~dps0 (short path of the current batch file's parent folder) for the path and %~nxs0 (current batch file's short file name and extension) for the file mask:

FORFILES.EXE /P %~dps0 /M %~nxs0 /C "CMD.EXE /C ECHO 0x1B[1;31m Bright Red Foreground 0x1B[0;41m Red Background 0x1B[0m Normal text"

 

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
foreground
Bold
foreground
Background
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
<Esc>[=0h
<Esc>[=0l
40x25 B/W text
<Esc>[=1h
<Esc>[=1l
40x25 color text
<Esc>[=2h
<Esc>[=2l
80x25 B/W text
<Esc>[=3h
<Esc>[=3l
80x25 color text
<Esc>[=4h
<Esc>[=4l
320x200 4 colors
<Esc>[=5h
<Esc>[=5l
320x200 2 colors
<Esc>[=6h
<Esc>[=6l
640x200 2 colors
<Esc>[=7h enable wrap
ANSI sequence Video mode
<Esc>[=7l disable wrap
<Esc>[=13h
<Esc>[=13l
320x200 16 colors
<Esc>[=14h
<Esc>[=14l
640x200 16 colors
<Esc>[=15h
<Esc>[=15l
640x350 2 colors
<Esc>[=16h
<Esc>[=16l
640x350 16 colors
<Esc>[=17h
<Esc>[=17l
640x480 2 colors
<Esc>[=18h
<Esc>[=18l
640x480 16 colors
<Esc>[=19h
<Esc>[=19l
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:

<Esc>[inkey;outstringp
inkey specifies the macro's hotkey
outstring specifies the keystrokes to be performed

 

Examples
inkey
code
outstring
code
ANSI
sequence
Result
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

ANSI Art

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:

TYPE AN_APPLE.ANS

Thanks Chris

Click to download source

 

Robert Van Etta found out that ANSI functionality is back in Windows 10.
To demonstrate it, he wrote Snake.bat, a "bouncing snake":

Screendump of Snake.bat

Take a closer look at this demo code, Robert has used a clever way to generate the Escape characters using FORFILES.

Thanks Robert

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: 2018-12-02, 18:17