Search notes:

ANSI escape sequences

ANSI Escape Sequences are instructions for consoles to position the cursor, change and set color and fonts and perform other operations.
These instructions are encoded in a series of characters whose first byte is the escape character (ASCII 27, octal \033, hex 0x1b).
Apparently, VT 100 influenced these instructions heavily.

Cursor Movements

The cursor can be moved with the following sequence: ^[[nx.
The value of n indicates how much the cursor moves. It is optional and defaults to 1.
x is A, B, C or D and indicates the cursor to move up, down, right or left, respectively.
$ echo -en '\e[4Afour lines above\e[20C20 characers to the right'
With x being E or F, the cursor is moved to the beginning of the nth next/previous line.
See also Absolutely positioning the cursor

Hiding and showing the cursor

^[[?25h hides the cursor, ^[[?25l shows it.

Colors

16 basic colors

There are eight colors, each of which is identified by a single digit:
0 black
1 red
2 green
3 yellow
4 blue
5 purple
6 cyan
7 white
Each of these colors has a high intensity variant, which brings the number of basic colors to 16 (and correspond to the values of the .NET enum System.ConsoleColor).
In order to create a color code, the color-digit needs to be prepended with one or two other digits (the dot represents the digit for the color):
3. foreground / normal intensity
4. background / normal intensity
9. foreground / high intensity
10. background / high intensity
Thus, for example a high intensity (=9) red (=1) text is escaped like so:
\e[91m text \e[0m
Or, … in a shell with echo:
$ echo -e "\e[91mred text\e[0m"
$ echo -e "\e[103m\e[91mred text on yellow background\e[0m"

Defining the RGB values for the 16 base colors

At least in conhost.exe, it's possible to define the RGB values for the 16 colors with \e]4;c;rgb:RR/GG/BB\x7. RR, GG and BB are the hex values for the RGB colors. c is the color number which is being defined. Note the closing bracket ].
The following PowerShell script modifies the color palette according to solarized light:
function define-color {
   param (
      [int] $colorNumber,
      [int] $r,
      [int] $g,
      [int] $b
   )

   write-host ("$([char]27)]4;{0};rgb:{1:X2}/{2:X2}/{3:X2}$([char]7)" -f $colorNumber, $r, $g, $b)
}

define-color  0    0 39   49  // dark black
define-color  1  208 27   36  // dark red
define-color  2  114 137   5  // dark green
define-color  3  165 119   5  // dark yellow
define-color  4   32 117 199  // dark blue
define-color  5  198 27  110  // dark magenta
define-color  6   37 145 133  // dark cyan
define-color  7  233 226 203  // dark white

define-color  8    0 77  100  // bright black
define-color  9  189 54   18  // bright red
define-color 10   70 90   97  // bright green
define-color 11   82 103 111  // bright yellow
define-color 12  112 129 131  // bright blue
define-color 13   88 86  185  // bright magenta
define-color 14  129 144 143  // bright cyan
define-color 15  252 244 220  // bright white

216 6-bit colors + 16 basic colors + 24 gray scale colors

With \e[38;5;nm (foreground) and \e[48;5;nm (background colors), the color palette can be extended to 256 colors which are specified by n:
n
0-7 8 colors
8-15 8 intense color
16 + 36*r + 6+g + b RGB value (each component having 6 values)
232-255 24 gray scale colors, black to white
The following shell snippet prints the color with R=5, G=2 and B=0:
$ r=5
$ g=2
$ b=0
$ color=$(( 16 + $r * 36 + $g * 3 + $b ))
$ echo -e "\e[38;5;${color}mr=$r g=$g b=$b\e[0m"

Demonstration: Emit all 6-bit RGB colors with a script.

The following shell script uses the 38;5 escape function to create 216 background colors (The red, green and blue components are each in the range 0 … 5)
#
#  vim: ft=sh
#
ansi_color_5() {
  local r=$1 # 0 .. 5
  local g=$2 # 0 .. 5
  local b=$3 # 0 .. 5

  echo -e $(( 16+ $r*36 + $g*6 + $b))
}

ansi_color_bg_5 () {
  echo -e "\e[38;5;$(ansi_color_5 $1 $2 $3)m"
}
ansi_color_fg_5 () {
  echo -e "\e[48;5;$(ansi_color_5 $1 $2 $3)m"
}

ansi_reset() {
  echo -e "\e[0m"
}


printf "\n"
for r in {0..5}; do
  for g in {0..5}; do
    for b in {0..5}; do

      printf "  "
    
      if [[ $r > 3 || $g > 3 ]]; then
        bg_color='0 0 0'
      else
        bg_color='5 5 5'
      fi
    
      printf "$(ansi_color_fg_5 $r $g $b)$(ansi_color_bg_5 $bg_color) %3d " $(ansi_color_5 $r $g $b)
      
    done
    printf $(ansi_reset)
    printf "\n"
  done
  printf "\n"
done
Github repository ANSI-escape-sequences, path: /bg-48-fg-38-5
The output of the script is
Apparently, this python script and this Perl script do more or less the same thing.
And then, there is also the shell command msgcat --color=test.

24-bit colors

With \e[38;2;r;g;bm and \e[48;2;r;g;bm, The color palette is extended to true colors, r, g and b being values between 0 and 255:
$ r=222
$ g=111
$ b=42
$ text="R = $r, G = $g, B = $b"
$ echo -e "\e[38;2;$r;$g;$b""m$text""\e[0m"

Positioning the cursor

Using H to position the cursor on specific coordinates.
The first number ist the y coordinate, the second number the x coordinate.
echo -e "\e[15;8Hx=8 / y=15"
echo -e "\e[4;22Hx=22 / y=4"
Github repository ANSI-escape-sequences, path: /position-cursor

Clearing the screen

echo -e "\e[2J"
Github repository ANSI-escape-sequences, path: /clear-screen

Resetting all settings

echo -e "\e[0m"
#
#   Might be followed by \e[2J (Clear screen)
#
Github repository ANSI-escape-sequences, path: /reset-all

Determine sequence for specific key on keyboard

In order to determine a key's emitted sequence, one can use ctrl-v followed by the key.

setterm

setterm is a utility that sets terminal attributes by writing a character string to stdout that invokes the specified terminal capabilities.
The following command writes the word Warning: in red:
$ echo $(setterm --foreground red)Warning:$(setterm --default) do not write too much text in red

PowerShell

In PowerShell, the escape character can be expressed by casting 27 or its hexadecimal representation 0x1b to a char:
write-host "$([char]0x1b)[40;91m red on black $([char]27)[0m"
PowerShell 7 (6?) shortens this cast to a backtick-e which is visually easier to read:
write-host "`e[32;103m green on intense yellow `e[0m"
See also ANSI colors in PowerShell.

See also

highlight.pl: a Perl script that prints ERROR in red and WARNING in yellow.
Perl module Term::ANSIColor
Coloring the output in Bash's echo
sed example to colorize a file.
tput: colors
Virtual Terminal Sequences on Windows
The PowerShell module console, especially its function get-ansiEscapedText.
In bash's echo command, the -e flag interprets \e as <ESC>.

Links

http://ascii-table.com/ansi-escape-sequences.php
Wikipedia: ANSI escape codes
Microsoft Documentation: Console Virtual Terminal Sequences

Index