Skip to content

teneplaysofficial/ansi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

ANSI

Style your terminal with ANSI magic ✨

ANSI escape codes are sequences of characters used to control the style, color, and behavior of text output in a terminal. They are widely supported across Unix terminals, macOS Terminal, modern Windows consoles (10+), and many terminal emulators like VS Code, iTerm2, Alacritty, etc.

What is an ANSI Escape Code?

An ANSI escape code starts with the escape character followed by a Control Sequence Introducer (CSI), then one or more codes (parameters), and ends with a command character.

Syntax

ESC [ parameters m
  • ESC is the escape character (ASCII 27 or \x1b)
  • [ is the Control Sequence Introducer (CSI)
  • parameters are semicolon-separated values (e.g., 1;31)
  • m indicates text formatting (SGR - Select Graphic Rendition)

This is used for text formatting. For example:

echo -e "\033[1;32mBold Green Text\033[0m"

Outputs Hello in red, then resets.

Escape Character ESC

All ANSI sequences begin with the escape character (byte 27 in decimal).

Representation Description
\x1b Hex notation (common in JS)
\033 Octal notation (common in Bash)
\u001b Unicode notation (JSON-safe)
\e Shorthand in Bash/Zsh

Style Codes (SGR - Select Graphic Rendition)

SGR parameters define text style, color, and effects.

Text Attributes

Code Style
0 Reset all
1 Bold
2 Dim
3 Italic
4 Underline
5 Blink (slow)
6 Blink (fast)
7 Reverse (invert fg/bg)
8 Hidden
9 Strikethrough

Reset Variants:

Code Resets
21 Bold/Double Underline
22 Bold + Dim
23 Italic
24 Underline
25 Blink
27 Inverse
28 Conceal
29 Strikethrough

Color Codes

Standard Colors (4-bit)

Foreground (Text)

Code Color Bright Code Bright Color
30 Black 90 Bright Black (Gray)
31 Red 91 Bright Red
32 Green 92 Bright Green
33 Yellow 93 Bright Yellow
34 Blue 94 Bright Blue
35 Magenta 95 Bright Magenta
36 Cyan 96 Bright Cyan
37 White 97 Bright White

Background Colors

Code Color Bright Code Bright Color
40 Black 100 Bright Black
41 Red 101 Bright Red
42 Green 102 Bright Green
43 Yellow 103 Bright Yellow
44 Blue 104 Bright Blue
45 Magenta 105 Bright Magenta
46 Cyan 106 Bright Cyan
47 White 107 Bright White

Extended Colors

256-Color Mode

Format:

  • Foreground: \033[38;5;<n>m
  • Background: \033[48;5;<n>m

Where <n> is from 0 to 255.

Color Breakdown

Range Meaning
0–15 Standard + Bright colors
16–231 6×6×6 RGB cube
232–255 Grayscale ramp (24 steps)
RGB Cube (16–231)

Each RGB channel (r, g, b) can be from 0–5 (6 levels).

Formula:

code = 16 + (36 × r) + (6 × g) + b

Example:

echo -e "\033[38;5;196mBright Red\033[0m"

Grayscale Ramp (232–255)

24 shades from near black to white, no color hue.

for i in {232..255}; do
  echo -ne "\033[48;5;${i}m \033[0m"
done

True Color (24-bit)

Format:

  • Foreground: \033[38;2;<r>;<g>;<b>m
  • Background: \033[48;2;<r>;<g>;<b>m

Where r, g, and b are 0–255.

Examples

echo -e "\033[38;2;255;140;0mOrange\033[0m"
echo -e "\033[48;2;0;0;64;38;2;255;255;255m White on Navy \033[0m"

Gradient Example

for i in {0..255..25}; do
  echo -ne "\033[48;2;${i};0;255m ${i} \033[0m"
done

Combining Multiple Codes

You can combine multiple SGR parameters with semicolons:

ESC="\033"

echo -e "${ESC}[1;4;31mBold Underlined Red${ESC}[0m"

Cross-Platform Support

ANSI codes are supported on:

  • Unix (Linux, macOS)

  • Windows 10+ (older versions require ANSICON or enabling VT)

  • Node.js (supports ANSI natively)

  • Browsers (via dev tools console with limited support)

  • Many programming languages:

    • Python: print("\033[31mRed\033[0m")
    • Bash: echo -e "\033[32mGreen\033[0m"
    • JavaScript: console.log("\x1b[36mCyan\x1b[0m")

Utilities

Reset Code

Always end ANSI sequences with reset to avoid carryover:

RESET = "\x1b[0m";

JavaScript

const style = (...codes: number[]) => `\x1b[${codes.join(";")}m`;

console.log(`${style(1, 34)}Bold Blue${style(0)}`);

Bash Function

color() { echo -e "\033[$1m$2\033[0m"; }
color "1;33" "Bold Yellow Text"

Cursor Control (Bonus)

Code Function
\x1b[H Move to home position
\x1b[2J Clear screen
\x1b[K Clear line to right
\x1b[<n>A Move cursor up
\x1b[<n>B Move cursor down
\x1b[<n>C Move cursor forward
\x1b[<n>D Move cursor backward

References

Releases

No releases published

Packages

No packages published

Languages