and their suggested use under Linux

(This page is now outdated)

FOREWORD : a note for linuxians   

Our text console is generally VT100 cmpatible, so ANSI compatible. The use of   SGR  codes (the ones usefull here), for the colors, allows us to design coloured texts or pseudo-graphics. Examples are a customized /etc/issue or a PS1 scripts.  There are this same codes that are used in   /etc/DIR_COLORS .

Here is an example of customized PS1 using ANSI codes, to be put in /etc/profile or /etc/bashrc

See also : man console_codes

PREFACE : note pour les linuxiens   

Notre console texte est en général compatible VT100, donc ANSI. L'utilisation des codes  SGR  (les seuls qui nous serons utiles ici) pour les couleurs permet de réaliser des textes ou des pseudo-graphismes colorés, par exemple un fichier /etc/issue ou un prompt PS1 personalisés. Ce sont aussi ces mêmes codes qui sont utilisés dans  /etc/DIR_COLORS .

Voici un exemple de PS1 personalisé avec des codes ANSI, à mettre dans /etc/profile ou /etc/bashrc

Voir aussi : man console_codes

if test "$UID" = 0; then
  PS1='[\033[1;31m\[email protected]\h \033[34m\w\033[0;37m]\n# '
  PS1='[\033[1;31m\[email protected]\h \033[34m\w\033[0;37m]\n$ '


ANSI driver allows application programs to utilize hardware dependent features and maintain compatibility between systems. To obtain access to the ANSI console and keyboard drivers under MS-DOS, the following command with the pertinent path must be added to the CONFIG.SYS file :


  1. The default value is used when no explicit value is given or a value of zero is given
  2. Pn is a numeric parameter, a decimal number specified with ASCII digits
  3. Ps is a selective parameter, any decimal number used to select a subfunction. Multiple subfunctions may be selected by separating the parameters with semi-colons.


In the following discussion of cursor functions

L = line number
C = column number
Ln = number of lines
Cn = number of comlums

The last letter of the syntax must be entered as shown

CUP - Cursor position : ESC[L;CH

HVP - Horizontal and Vertical position : ESC[L;CF

CUP and HVP move the cursor to the position specified, the default value is one. If no parameters are given the cursor is moved to the home position.

CUU - Cursor Up : ESC[LnA

CUU moves the cursor up without changing columns, the default value is one, this sequence is ignored if the cursor is already on the top line.

CUD - Cursor Down : ESC[LnB

CUD moves the cursor down without changing columns, the default value is one, this sequence is ignored if the cursor is already on the bottom line.

CUF - Cursor Forward : ESC[CnC

CUF moves the cursor forward without changing lines, the default value is one, ignored if the cursor is in the rightmost column.

CUB - Cursor Backward : ESC[CnD

CUB moves the cursor Backward without changing lines, the default value is one, ignored if the cursor is in the rightmost column.

DSR - Device Status Report : ESC[6N

The console driver will output a CPR sequence on receipt of DSR (see below)

CPR - Cursor Position Report : ESC[Ln;CnR

The CPR sequence reports current cursor position (from console driver to system) via standard input, Ln and Cn are respectively the current line and column.

SCP - Save current Position : ESC[S

The cursor position is saved and can be restored with the RCP sequence.

RCP - Restores Current Position : ESC[U

Restores the cursor position to the value saved with the SCP sequence.


ED - Erase Display : ESC[2J

Erases the screen and the cursor goes to the home position.

EL - Erase Line : ESC[K

Erases from the cursor position to the end of the line, including cursor position.


SGR - Sets Graphics Rendition : ESC[Ps;.....Ps m

Invoques the graphic rendition specified by the parameter(s), all following characters are rendered according to the parameter(s) until the next occurence of SGR.

Parameter Function		Comments

0 	  All attributes Off

1 	  Bold On

3  	  Underscore On 	Monochrome display only

5 	  Blink On

7 	  Reverse video On

8 	  Concealed On    	IS0 6429 standard

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         	"

SM - Set Mode : ESC[=Psh 

  	     or ESC[=h

             or ESC[=0h    

             or ESC[?7h

Invoques the screen width or type specified by the parameter.

Parameter Function

0 	  40 x 25 black and white

1 	  40 x 25 color

2 	  80 x 25 black and white

3 	  80 x 25 color

4 	  320 x 200 color

5 	  320 x 200 black and white

6 	  640 x 200 black and white

7 	  wrap at end of line

RM - Reset Mode : ESC[=Psl

  	       or ESC[=l

  	       or ESC[=0l

  	       or ESC[?7l

Parameters are the same as for Set Mode, except that parmeter 7 will reset wrap at end of line.


Althought not part of ANSI 3.64 1979 or ISO 6429 standard, the keyboard reassignment was done in a compatible way.

The control sequence is : ESC[Pn;Pn;....Pnp

  or ESC["string";p

  or ESC[Pn;"string";Pn;Pn;"string";Pn p

or any combination of strings and decimal numbers

The final code (p) is one reserved for private use by the ANSI 3.64 1979 standard. The first ASCII code in the control sequence defines which code is being mapped, the remaining numbers define the sequence of ASCII codes generated when the key is intercepted. As an exception to the rule, if the first code in the sequence is a zero (NULL) then the first and second codes make up an extended ASCII re-definition.

Example 1

Reassign Q and q keys to the A and a keys (and the other way as well)

  ESC[65;81p      A becomes Q

  ESC[97;113p     a becomes q

  ESC[81;65p      Q becomes A

  ESC[113;97p     q becomes a

Example 2

Reassign the F10 key to a DIR command followed by CR


The 0;68 is the extended ASCII code for the F10 key, 13 decimal is a carriage return

File: ansidrv.html - Robert L.E. Billon, 2000-03-15 - Last update: 2011-09-18