ANSI escape code
From Free net encyclopedia
←Older revision | Newer revision→
ANSI escape codes are used to control text formatting and other output options on text terminals that are based on the ANSI X3.64 (or ECMA-48) standard. All these escape sequences start with the characters ESC (ASCII 27d / 1Bh / 033o ) and [ (left bracket). This sequence is called CSI for "Control Sequence Introducer".
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 C, [[C++]], Java, Python or Perl this is "\x1B", in QBasic or GWBASIC, the code would be CHR$(27) or CHR$(&H1B) (for those of you who prefer hexadecimal). Support is built in on Windows and Linux, DOS systems need a device driver for the ANSI codes — the de facto standard being ANSI.SYS
, but others are used as well.
Code | Effect |
---|---|
CSI n A | 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. |
CSI n B | 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. |
CSI n C | Moves the cursor right n columns. If the cursor is already at the edge of the screen, this has no effect. If n is not given, 1 is used. |
CSI n D | Moves the cursor left n columns. If the cursor is already at the edge of the screen, this has no effect. If n is not given, 1 is used. |
CSI n ; m f | Moves the cursor to row n, column m. If n is not given, 1 is used. |
CSI n ; m H | Moves the cursor to row n, column m. If n is not given, 1 is used. |
CSI = n h | Set screen mode. (MS-DOS ANSI.SYS) |
CSI n J | Clears part of the screen. If n is zero, clear from cursor to end of screen. If n is one, clear from cursor to beginning of the screen. If n is two, clear entire screen. (Moves cursor to upper left on MS-DOS ANSI.SYS) |
CSI n K | Erases part of the line. If n is zero, clear from cursor to the end of the line. If n is one, clear from cursor to beginning of the line. If n is two, clear entire line. |
CSI = n l | Reset screen mode on MS-DOS ANSI.SYS. |
CSI n ... k m | Sets graphics parameters. |
CSI code ; param [ ; param ] p | Redefines a key. |
CSI 6 n | Reports the cursor position to the application as (as though typed at the keyboard) ESC[n;mR, where n is the row and m is the column. (May not work on MS-DOS ANSI.SYS) |
CSI s | Saves the cursor position. |
CSI u | Restores the cursor position. |
Code | Effect |
---|---|
0 | 40 × 25 mono |
1 | 40 × 25 color |
2 | 80 × 25 mono |
3 | 80 × 25 color |
4 | 320 × 200 color |
5 | 320 × 200 mono |
6 | 640 × 200 mono |
7 | Wrap at end of line |
Code | Effect |
---|---|
0 | All attributes off |
1 | Bold |
2 | Faint (not widely supported) |
3 | Italic (not widely supported) |
4 | Underlined (not widely supported) |
5 | Blink |
6 | Rapid blink (MS-DOS ANSI.SYS) |
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 (not widely supported) |
49 | Superscript (not widely supported) |
Examples
CSI 0 ; 6 8 ; "DIR" ; 13 p - 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. (MS-DOS ANSI.SYS only)
CSI 2 J - This clears the screen and locates the cursor to the y,x position 0,0 (upper left corner).
CSI 32 m - This makes text green. Normally the green would be dark, dull green, so you may wish to enable Bold with the code CSI 1 m which would make it bright green. This can also be accomplished with CSI 32 ; 1 m
CSI s - This saves the cursor position. Using the code CSI u will restore it to the position. Say the current cursor position is 7(y) and 10(x). The code CSI 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 CSI 20 ; 3 H or CSI 20 ; 3 f. Now if you use the code CSI u the cursor position will return to 7(y) and 10(x). Some terminals require the DEC sequences ESC 7 / ESC 8 instead.
See also
External links
- Standard ECMA-48: Control Functions for Coded Character Sets 5th edition (June 1991)fi:ANSI X3.64