In this chapter
The term ANSI refers to the American National Standards Institute. ANSI is one of several sets of computer standards established by the institute to specify the codes that computer manufacturers can use to control video displays and keyboard mapping.
ANSI.SYS
is a device driver, which means that it gives DOS additional control of the screen and keyboard devices beyond the control features built into the operating system. You use ANSI.SYS
to enhance the functions of your video screen and keyboard. With ANSI.SYS
, you can set screen colors, use graphics, and specify other video attributes. You can provide that personal touch to your DOS prompt. You even can change the assignments of keys on your keyboard.
The ANSI.SYS
file supplied with MS-DOS contains a subset of the ANSI standards. Third-party suppliers of other ANSI.SYS
files might include more features in their versions of this file, but those features are not necessary for most users.
The only way you can install the ANSI.SYS
driver is to include it in your CONFIG.SYS
file. The format of the line must be one of the following:
DEVICE = C:DOSANSI.SYS /X /K /R DEVICEHIGH = C:DOSANSI.SYS /X /K /R
The /X
switch enables you to remap extended keys if you are using a 101-key keyboard. The /K
switch treats 101-key keyboards as if they were 84-key keyboards, ignoring extended keys. The /R
switch slows screen scrolling for improved readability.
The /X
and /K
switches are mutually exclusive because you cannot define a 101-key and 84-key keyboard at the same time.
When you install MS-DOS on your computer, DOS places the ANSI.SYS
file in the DOS
subdirectory. If ANSI.SYS
is located in the root directory of your boot drive, you don't have to include its drive and directory name in the command line. If you placed the ANSI.SYS
file in another directory or on another drive, however, be sure that you specify the exact location of ANSI.SYS
in the CONFIG.SYS
statement.
In the following sections, you will learn how to use the ANSI.SYS driver and take advantage of its features.
You must place the ANSI.SYS
command in the CONFIG.SYS
file to load the driver and reboot the system so that the new configuration takes effect, you must issue commands that tell DOS to use the ANSI features.
Because some software is written to use ANSI codes, one or more of your programs might require that ANSI.SYS
be loaded. The installation procedure for this type of software notifies you of this requirement. (The program might even install the command in the CONFIG.SYS
file for you.)
You can activate ANSI.SYS
features also by issuing the ANSI commands yourself. However, you cannot simply type the commands at the DOS prompt; you must enter an Escape sequence. All ANSI.SYS
sequences begin with the Escape character (ASCII value 27), followed by a left bracket ([
). If you type this sequence at the prompt, DOS understands the Escape character to be a command to cancel the current operation; DOS cancels the operation, displays a backslash (), and moves the cursor down to the next line.
If ANSI.SYS
codes are not included in a program, you can send these codes to DOS in three ways: by executing a batch file, by typing a text file (that is, by using the DOS TYPE
command), or by including the codes in a PROMPT
command. When you embed ANSI codes in Escape sequences, the ANSI.SYS
device driver intercepts the codes and executes the appropriate commands, ignoring any characters that are not preceded by the proper codes.
You can create a text file or batch file or set up your PROMPT
format in the AUTOEXEC.BAT
file by using the MS-DOS editor (EDIT
) or another text editor. Whatever utility you use must be capable of entering the Escape character, which is beyond the capability of many word processing programs and older text editors.
To enter the Escape character while using EDIT
, hold down the Ctrl key, press P, and then press Esc.
→ For information on using EDIT
, see Chapter 15, “Using the DOS Editor,” p. 361.
Some text editors display the Escape character as ^[
. Because the ANSI code sequence requires a left bracket after the Escape character, the ANSI sequence might appear as ^[[
.
The key to using ANSI.SYS
is to have the Escape sequences sent to the display screen. This procedure is the only way to ensure that the device driver properly intercepts and executes the commands. Batch files send these sequences to the screen through the ECHO
command, which tells DOS to display all commands on the screen.
→ For more information on batch files, see Chapter 16, “Understanding Batch Files,” p. 389.
By default, ECHO
is set ON
unless you explicitly issue an ECHO OFF
command. If you do use ECHO OFF
, any ANSI code line you enter thereafter must begin with the ECHO
command.
Suppose that you want to use a batch file to set up formatting for a double-density disk (360KB) in a 5 1/4-inch high-density (1.2MB) drive. You use EDIT
to create the batch file containing the instruction for the special effect, such as changing the color of the screen. Entering the following batch file and giving it the name NEWDISK.BAT
do the trick:
@ECHO OFF ECHO <Esc>[37;41m ECHO THIS FUNCTION FORMATS DOUBLE DENSITY DISKETTES (360K) IN DRIVE A: FORMAT A: /F:360 ECHO <Esc>[37;40m
The first line in the batch file tells DOS not to display any of the commands in the batch file unless specifically instructed to do so through the ECHO
command. The leading character (@
) instructs DOS not to display the ECHO OFF
command.
The second line is an ANSI.SYS
code sequence that sets the color of the screen (red, with white characters). Replace <Esc>
with the actual Escape code (ASCII value 27). The ANSI code 37 produces a white foreground; 41 is the ANSI code for a red background. The character m
indicates that screen attribute codes are being issued. The last line of the batch file resets the screen to white on black—the default setting. (Make sure that you use the lowercase character m
in the ANSI.SYS
Escape sequences.)
If you have a monochrome monitor, you can substitute 7m
for 37;41m
in the second line of the batch file. The code 7 tells ANSI.SYS
to set reverse video. If you use 7m
instead of 37;41m
, substitute 0m
for 37;40m
in the last line to set the screen back to normal.
To see why you must use the ECHO
command in the ANSI Escape sequence, remove the ECHO
command from the last line of the batch file, and then run the file again. The ECHO
command is required to pass the codes to the display. If it is not available, DOS informs you that it received a bad command or filename, and the screen color does not change to normal.
Issuing ANSI codes in text files is similar to using the codes in batch files, except that you do not use an ECHO
command in a text file. Also, for the commands to take effect, you must use the TYPE
command to pass the codes to the display for DOS to execute. To look at a sample text file, create a SCREEN.TXT
file that contains the following line:
<Esc>[37;41m
After you create and save this file, you can change the screen to red with white characters by entering the following command at the DOS prompt:
TYPE SCREEN.TXT
ANSI.SYS
intercepts the characters of the file when being sent to the screen with the TYPE
command. The Escape sequence, followed by the bracket, causes the device driver to execute the ANSI commands instead of passing the characters through to DOS. Your screen is now red with white characters.
You can return your screen to normal as follows. In the same way that you created SCREEN.TXT
, create NORMAL.TXT
with the following single line:
<Esc>[37;40M
After you enter this line, save the file. At the DOS prompt, enter the following to return your screen to normal:
TYPE NORMAL.TXT
The third way to issue ANSI.SYS
instructions is to include these instructions in a PROMPT
command. You learned about the PROMPT
command in Chapter 11, “Controlling Your Environment.” In short, the PROMPT
command alters the way the DOS prompt appears. The normal syntax of the command is as follows:
PROMPT string
The string
consists of a set of characters that might or might not include an ANSI Escape sequence. The characters you enter tell DOS how you want the DOS prompt to look. For full information on the characters you can use with the command, refer to Chapter 11. This section focuses on using the ANSI sequences with the PROMPT
command.
To add more pizzazz to your DOS prompt, you can add color, as outlined in the following examples. If you have a color monitor, enter the following command, using nine spaces for the nine periods:
PROMPT $E[1;37;44mTime: $T$H$H$H$H$H$H.........$E[40m$E[K$_$E[1;44mDate: $D$E[0;40m$E[K$_[DOS 6.2] $P$G
The display shows the time and date in bright white text on a blue background, returning to normal white on black for the prompt.
If you have a monochrome monitor, enter the following command, using nine spaces for the nine periods:
PROMPT $E[7mTime: $T$H$H$H$H$H$H.........$E[0m$E[K$_$E[7mDate: $D$E[0m$E[K$_[DOS 6.2] $P$G
The display now shows the time and date in black characters on a white background, returning to normal white on black for the prompt.
As the inclusion of [DOS 6.2]
in these two examples indicates, you also can display a message in the DOS prompt. (The brackets are included for cosmetic purposes only. They are not required.) Be creative. Beam me up, Scotty; The BRAIN; USA Forever; and BOOM! are a few examples of DOS users' humor and imagination.
Now that you understand how to issue ANSI.SYS
commands in batch files, in text files, and as part of a PROMPT
command, you're ready to use ANSI.SYS
commands to control and customize your screen display.
All ANSI screen commands begin with the normal Escape code and a left bracket ([
) and end with a letter of the alphabet. The letter depends on the type of command you are asking ANSI.SYS
to execute. In all instances, the capitalization of the ending letter is important. As you are working through the examples in this chapter, pay strict attention to the capitalization.
You can use as many ANSI control codes as you require on the same line, as long as the codes are separated by semicolons (;
) and no code contradicts another code. The order of the codes is not important.
The following sections detail the general functions that can be performed with ANSI.SYS
.
If you end the Escape sequence with the uppercase letter A, ANSI.SYS
assumes you want to move the cursor up a given number of lines on the screen. For instance, the following line moves the cursor up four lines on the screen:
<Esc>[4A
The horizontal position of the cursor is not affected; it only moves up. If the cursor is already at the top of the screen, the Escape code is ignored.
If you end the Escape sequence with a B (instead of an A), the cursor is moved down on the screen. Again, if the cursor is already at the bottom of the screen, the sequence is ignored.
Ending the Escape sequence with a C moves the cursor to the right by the number of indicated spaces; a D moves the cursor left. For example, the following line moves the cursor 23 spaces to the right:
<Esc>[23C
To position the cursor at a given character location on the screen, you can use either the H or f codes. For instance, either of the following moves the cursor to line 10, column 32:
<Esc>[10;32H <Esc>[10;32f
Note that the upper-left corner of the screen is considered position 0,0.
To save the current cursor position (in preparation for moving and later restoring it), you use the s code, as shown here:
<Esc>[s
There are no other parameters; the cursor position is immediately saved. When you want to restore the cursor to the saved position, use the u code:
<Esc>[u
You can use ANSI.SYS
to change the display mode of your system. You do so by providing a screen code and ending the sequence with the h code. The screen codes are listed in Table 17.1.
Table 17.1. ANSI Screen Display Codes
Code | Screen Type |
---|---|
0 | 40×25 characters, monochrome |
1 | 40×25 characters, color |
2 | 80×25 characters, monochrome |
3 | 80×25 characters, color |
4 | 320×200 pixels, monochrome |
5 | 320×200 pixels, color |
6 | 640×200 pixels, monochrome |
7 | Turns word wrap on and off |
14 | 640×200 pixels, color |
15 | 640×360 pixels, monochrome |
16 | 640×360 pixels, color |
17 | 640×480 pixels, monochrome |
18 | 640×480 pixels, color |
19 | 320×200 pixels, color |
For example, the following line sets the screen mode to 80×25 color text:
<Esc>[3h
You can end the sequence also with a lowercase l; the only difference is in how screen code 7 is handled. With the h code, line wrap is turned on; with l, it is turned off. For instance, the following turns on 80×25 monochrome text with screen wrapping disabled:
<Esc>[2;7l
Text attributes include color, bold, or reverse video. To set these attributes, list the desired attribute codes, separated by semicolons, and then terminate the Escape sequence with the m code. Table 17.2 lists the ANSI codes for setting the character attributes.
Table 17.2. Character Attribute Codes
Code | Effect |
---|---|
0 | Normal display (the default) |
1 | High-intensity text |
4 | Underlined text (monochrome) |
5 | Blinking text |
7 | Reverse video (black on white) |
8 | Hidden text (black on black) |
30 | Black foreground (character) color |
31 | Red foreground (character) color |
32 | Green foreground (character) color |
33 | Yellow foreground (character) color |
34 | Blue foreground (character) color |
35 | Magenta foreground (character) color |
36 | Cyan foreground (character) color |
37 | White foreground (character) color |
40 | Black background color |
41 | Red background color |
42 | Green background color |
43 | Yellow background color |
44 | Blue background color |
45 | Magenta background color |
46 | Cyan background color |
47 | White background color |
Some screen effects depend on the hardware. Underlined text, for example, cannot be displayed on all monitors.
You can use the codes in Table 17.2 to customize your system. You can, for example, use ANSI codes to set the default screen colors to bright green on black:
<Esc>[1;32;40m
Besides the functions discussed so far, ANSI.SYS
also provides functions that you can use to control your screen. You can use the following ANSI code to clear the screen. It is the equivalent of the CLS
command:
<Esc>[2J
If you want to erase only to the end of the current line, the following does the trick:
<Esc>[K
Computers use a set of 256 codes to indicate specific characters. These codes make up the ASCII character set. (ASCII is an acronym for the American Standard Code for Information Interchange.) A space, for example, is ASCII code 48; the uppercase A is ASCII code 65; and the lowercase a is ASCII code 97. You already know that the Escape code used in an ANSI control sequence has an ASCII value of 27. (See Appendix E,“ASCII and Extended ASCII Codes,” for a list of ASCII codes.)
Every time you press a key on your keyboard, the system's circuits send a code to DOS. This code interprets the keystroke and displays the appropriate character. Not every code in the ASCII set, however, has a corresponding keyboard key. Moreover, some keys represent more than one character.
Keyboards actually send individual codes to DOS for each key or key combination you press on the keyboard. These codes are known as scan codes. Many scan codes correspond to ASCII codes. With the key combinations that involve the Ctrl, Alt, Shift, and Num Lock keys, however, more than 256 keystrokes (the ASCII code limit) are possible. This problem is solved by preceding some scan codes with a 0 entry.
When DOS receives a scan code from the keyboard, the program uses a built-in table to ascertain the proper character. When the ANSI.SYS
device driver has been loaded through the CONFIG.SYS
file, the ANSI driver takes over this chore. The driver also enables you to modify the table and assign different characters to the scan codes.
Suppose that you must prepare documents or data files that include fractions. You can assign ASCII codes for some fractions to keys that you normally don't use. ASCII code 171, for example, stands for 1/2, and code 172 stands for 1/4. (See Appendix E for a complete list of ASCII codes.) Using ANSI.SYS
makes key reassignments easy.
To assign the 1/2 fraction (ASCII 171) to the Shift+6 key combination, which produces the character ^ (ASCII 94), you can use either of the following commands:
<Esc>["^";"1/2"p <Esc>[94;171p
The ANSI.SYS
keyboard-assignment Escape sequence starts with the standard Escape character, followed by a left bracket ([
). The next character is the ASCII code, scan code, or key representation in quotation marks. Then you specify the new ASCII code or key representation in quotation marks. The two characters are separated by a semicolon. The sequence ends with the lowercase p code. Thus, in the first example, the key representation ^ is entered with quotation marks ("^"
) and is changed to "1/2"
. In the second example, scan code 171 replaces scan code 94.
Do not use spaces in the ANSI.SYS
code sequence. If you use spaces, DOS might not interpret the sequence properly.
Remember that the first specification is for the key to be assigned. To restore the original key assignment, enter the code twice, as in this example:
<Esc>[94;94p
A key assignment does not need to be a single character. You also can assign a text message to a keystroke. This process is referred to as a macro substitution. If you often need to type the same sequence of characters, you can easily set up a macro substitution that enters those characters for you. Don't confuse this with the macro capability of DOSKEY
, however. Macros that can be created with DOSKEY
are much more powerful than those created with ANSI.SYS
.
In the following example, the words Heigh-Ho, Silver! are assigned to the F7 key, which has the scan code 0;65
:
<Esc>[0;65;"Heigh-Ho, Silver!"p
After you issue this ANSI.SYS
instruction, either in a text file you type or a batch file, DOS (through ANSI.SYS
) displays the message Heigh-Ho, Silver!
whenever you press F7.
Because no scan code or ASCII code of 0 exists, ANSI.SYS
understands that the second number indicates the extended scan code for the F7 key instead of the ASCII code for the uppercase A, which is 65. If you want to add a carriage return (ASCII code 13) after the message, include that code in the Escape sequence as follows:
<Esc>[0;65;"Heigh-Ho, Silver!";13p
You can enter many of the ASCII character codes on your keyboard by holding down the Alt key while you type the ASCII code for the character you want to display. If you reassign keys, however, you no longer can enter certain key characters in this manner. Further, reassigned keys no longer function the same way. If you reassign the backslash key (), for example, you no longer can use the backslash character to access subdirectories.
All ANSI commands are prefixed by two characters: the Escape character (ASCII 27) and the left square bracket (ASCII 91). Because the Escape keypress usually is interpreted as a command, inserting it in a text file might require you to enter a special command, such as Ctrl+P, before your text editor will accept it as text.
Uppercase and lowercase are significant in these commands, so you must type them exactly as indicated. In commands that allow a variable number of codes, separate each code with a semicolon.
Set mode codes end in a lowercase h as shown. Reset mode codes substitute a lowercase l for the h. Not all displays support all screen modes, and other modes might be available for your display:
Background color, foreground color, and screen attribute codes may be combined in a single command. If any of the choices conflict, the rightmost color or attribute in the command is set. Not all colors or attributes are available on all displays:
<Esc>[2J
| Clears the entire screen |
<Esc>[K
| Clears from the cursor to the end of the line |
<Esc>[=7h
| Enables line wrapping |
<Esc>[=7l
| Disables line wrapping |
By using the following ANSI command, you can redefine the entire keyboard or set simple text macros for use at the command line. Some software application programs ignore keyboard reassignments set this way.
<Esc>[keycode;keytext;...p
keycode
indicates one of the key codes listed in the following table. Some keys use two codes, in which case both must be entered, separated by a semicolon as shown. Keycodes that appear in parentheses might not be available on all keyboards. (Do not include the parentheses.) Also, some keyboards use different codes for certain keys. Check your computer's documentation.
keytext
is either the ASCII code for a single character or text enclosed in double quotation marks. For example, both 83 and “S” can be used to represent an uppercase S. “Hello” also is allowed; it returns the full word when the key is pressed.
In the following table, keys indicated as (gray) refer to the gray-colored cursor pad area of the keyboard. Keys indicated as (keypad) refer to the numeric keypad area. Key codes that appear in parentheses are not available on all keyboards, and ANSI.SYS
might not interpret them if the /X
switch is not specified in the device command that loaded it. A blank entry indicates that this keyboard combination doesn't return a valid code.
3.22.181.47