Rob van der Woude's Scripting Pages

Enhancing your PROMPT with ANSI sequences

The most commonly used prompt string is probably $P$G, which results in a prompt like this:

C:\>_

However, with a little help from ANSI and some imagination, you can liven up your prompt.
You can use any string you like for a prompt, though long strings may prove to be quite impractical.

Note: ANSI is not available by default in 32-bit or 64-bit Windows, only in (DOS based) 16-bit Windows (95/98/ME).

 

Special PROMPT codes:

Code Result
$A &
$B |
$C (
$D Current date
$E Escape character
$F )
$G >
$H Backspace
(ignored if redirected)
$I Info bar (OS/2)
$L <
$M UNC name for current drive if remote (NT)
$N Current drive
$P Current directory
$Q =
$R Error level (OS/2)
$S Space (NT)
$T Current time
$V Operating system version
$X Operating system revision level (OS/2 DOS prompt)
$_ New line
$$ $
$+ A series of "+" signs corresponding to the number of pushed directories on the PUSHD stack (NT)

It is options like $B, $L and $G that allow us to abuse the PROMPT command, as is shown in some extreme batch file examples.

 

Date/Time with PROMPT

PROMPT's $D and $T options can be utilized to store the week day, current date and current time in environment variables, independent of the operating system's language! See the Date/Time page to find out how this can be achieved.

 

Fancy prompt example:

PROMPT  $E[0;1;33m$D$_ $T$H$H$H$H$H$H$E[31m   [$P]$E[0m

The prompt command shown here will make your prompt look like this:

 Thu 11/21/24
 10:41   C:\>_

 

Useful OS/2 prompt example:

PROMPT $V $X$_Errorlevel$Q$R$_[$P]

The prompt command shown here will make your OS/2 prompt look like this:

Operating System/2 version 4.00
Errorlevel=0
[C:\OS2]_

An OS/2 DOS prompt, however, will look like this:

Operating System/2 version 4.00 Revision 9.30
Errorlevel=0
[C:\OS2]_

 

A very fancy prompt:

PROMPT $E[s$E[1;1H$E[0;1;33;41m$E[K $P$G$E[1;54H$D $T$H$H$H$H$H$H$E[36;40m$E[u $P$G $E[37m

Depending on your browser settings the above line may not be shown as a single command line.
However, if you use this example in your AUTOEXEC.BAT, it should be typed as a single command line.
Also remember that DOS poses a 127 bytes limit on any command line, so don't overdo it.

The ANSI sequences in this example will do the following:

  1. save the current cursor position with $E[s
  2. move the cursor to the top left position of the screen with $E[1;1H
  3. reset all text attributes and set text colors to (bright) yellow on red with $E[0;1;33;41m
  4. erase the line with $E[K, thereby setting the color attributes for the whole top line
  5. show an "ordinary" prompt (like C:\>) with $P$G
  6. move the cursor to position (column) 54 on the line with $E[1;54H
  7. show current date and time with $D $T
  8. erase seconds and hundredth of seconds with 6 BackSpaces ($H$H$H$H$H$H)
  9. set text color to cyan (bright cyan, since the Bright attribute wasn't reset yet) on black with $E[36;40m
  10. restore the saved cursor position with $E[u
  11. show an "ordinary" prompt (in bright cyan) with $P$G
  12. and finally set text color back to (bright) white with $E[37m

The result will be an "ordinary" C:\> prompt in bright cyan at the normal position plus a red bar at the top of the screen, looking somewhat like this:

C:\>                                                  Thu 11/21/24  10:41       


C:\>_

page last modified: 2016-09-19; loaded in 0.0018 seconds