ANSI escape code

ANSI escape codes are used to control text formatting and other output options on some text terminals. All these escape sequences start with the characters ESC (ASCII 27d / 1Bh) 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). DOS systems need a device driver for the ANSI codes — the defacto standard being ANSI.SYS, but others are used as well.

Some ANSI codes
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 [nJ 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.
ESC [nK 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.
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.
Screen modes
Code Effect
0 40 × 25 mono
1 40 × 25 colour
2 80 × 25 mono
3 80 × 25 colour
4 320 × 200 colour
5 320 × 200 mono
6 640 × 200 mono
7 Wrap at end of line
Graphics parameters
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).

See also

External Links

  • Standard ECMA-48 (http://www.ecma-international.org/publications/standards/Ecma-048.htm): Control Functions for Coded Character Sets 5th edition (June 1991)
Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools