ANSI escape code
ANSI escape codes are used to specify visual effects on text terminals. They are typically initiated with the characters \e (backslash-'e') and [ (left bracket). From the MS-DOS prompt command, the escape character can be specified as $e, e.g. prompt $e[44m;... would specify a blue background as part of the prompt. In QBasic or GWBASIC, the code would be CHR$(27) or CHR$(&H1B) (for those of you who prefer hexadecimal). (27d or 1Bh is the same code as the keyboard code when you press the button ESC on your keyboard.) DOS systems need a driver for the ANSI codes - typically ansi.sys, but there are others used.
Code | Effect |
---|---|
ESC [nA | Moves the cursor up n rows. If the cursor is already at the top of the screen, this has no effect. If n is not given, 1 is used. |
ESC [nB | Moves the cursor down n rows. If the cursor is already at the bottom of the screen, this has no effect. If n is not given, 1 is used. |
ESC [nC | Moves the cursor right n rows. If the cursor is already at the edge of the screen, this has no effect. If n is not given, 1 is used. |
ESC [nD | Moves the cursor left n rows. If the cursor is already at the edge of the screen, this has no effect. If n is not given, 1 is used. |
ESC [n;mf | Moves the cursor to row n, column m. If n is not given, 1 is used. |
ESC [n;mH | Moves the cursor to row n, column m. If n is not given, 1 is used. |
ESC [=nh | Set screen mode. |
ESC [2J | Clears screen and sends cursor home. |
ESC [K | Erases to end of the line. |
ESC [=nl | Reset screen mode. |
ESC [n...km | Sets graphics parameters. |
ESC [code;param[;param]p | Redefines a key. |
ESC [n;mR | Sets the cursor to row n, column m, and reports this to the system. |
ESC [s | Saves the cursor position. |
ESC [u | Restores the cursor position. |
Code | Effect |
---|---|
0 | 40 x 25 mono |
1 | 40 x 25 colour |
2 | 80 x 25 mono |
3 | 80 x 25 colour |
4 | 320 x 200 colour |
5 | 320 x 200 mono |
6 | 640 x 200 mono |
7 | Wrap at end of line |
Code | Effect |
---|---|
0 | All attributes off |
1 | Bold |
2 | Faint |
3 | Italic |
5 | Blink |
6 | Rapid blink |
7 | Reverse video |
8 | Concealed |
30 | Black foreground |
31 | Red foreground |
32 | Green foreground |
33 | Yellow foreground |
34 | Blue foreground |
35 | Magenta foreground |
36 | Cyan foreground |
37 | White foreground |
40 | Black background |
41 | Red background |
42 | Green background |
43 | Yellow background |
44 | Blue background |
45 | Magenta background |
46 | Cyan background |
47 | White background |
48 | Subscript |
49 | Superscript |
Examples: ESC[0;68;"DIR";13p - This re-assigns the key F10 to send to the keyboard buffer the string "DIR" and ENTER, which in the DOS command line would display the contents of the current directory.
ESC[2J - This clears the screen and locates the cursor to the y,x position 0,0.
ESC[32m - This makes text green. Normally the green would be dark, dull green, so you may wish to enable Bold with the code ESC[1m which would make it bright green.
ESC[s - This saves the cursor position. Using the code ESC[u will restore it to the position. Say the current cursor position is 7(y) and 10(x). The code ESC[s will save those two numbers. Now you can move to a different cursor position, such as 20(y) and 3(x), using the code ESC[20;3H or ESC[20;3f. Now if you use the code ESC[u the cursor position will return to 7(y) and 10(x).