ANSI Color Code Generator

Build ANSI escape sequences for colored terminal output with a live preview. Pick foreground and background from the 16 standard ANSI colors, add attributes like bold, underline or italic, enter sample text, and get copy-ready sequences in bash (\033[), Python, Node.js, and raw numeric form. No more guessing SGR codes.

How to use the ANSI Color Code Generator

Select a foreground (text) color and optionally a background color from the 16 standard ANSI colors. Tick any style attributes: Bold makes text heavier, Dim reduces intensity, Italic is slanted (support varies), Underline adds an underline, Blink flashes the text (support varies by terminal), Reverse swaps foreground and background colors.

The live preview box shows how the styled text will appear in a dark terminal. Enter your own sample text in the text field to preview a realistic message.

Below the preview, the generator outputs:

  • Bash (\033[): embed directly in echo -e or printf
  • \e[ notation: shorter form, works in most shells
  • \x1b[ notation: works in Python strings without extra flags
  • Python: ready to use with print()
  • Node.js: ready to use with process.stdout.write() or console.log()
  • Raw SGR codes: just the numeric codes separated by semicolons

The reset sequence \033[0m is always appended to return the terminal to its default state after the text.

About ANSI Escape Codes

ANSI escape codes are standardised sequences of bytes that terminals interpret as control commands rather than printable characters. They originate from the ANSI X3.64 standard (1979) and the VT100 terminal. The most common type is the SGR (Select Graphic Rendition) sequence: ESC[nm where n is a semicolon-separated list of attribute codes. ESC is ASCII 27 (0x1B), written as \033 in octal, \x1b in hex, or \e in some contexts.

The 16 standard ANSI colors correspond to SGR codes 30-37 for normal foreground, 40-47 for background, and 90-97 / 100-107 for "bright" (high-intensity) variants. Exact color appearance depends on the terminal emulator's theme — a terminal showing "red" (31) as orange or dark red is within spec. For precise colors, use 256-color mode (38;5;N) or 24-bit truecolor (38;2;R;G;B).

Attribute codes include 1 (bold/bright), 2 (dim/faint), 3 (italic — not widely supported), 4 (underline), 5 (slow blink — many terminals ignore this), 7 (reverse video — swap fg/bg), and 9 (strikethrough). Reset code 0 clears all attributes. Every styled string should end with \033[0m to prevent color bleeding into subsequent terminal output.

Common use cases

  • CLI tool output coloring — highlight errors in red, warnings in yellow and success messages in green to improve readability of terminal output.
  • Shell script prompts — color the PS1 prompt to distinguish production from staging environments or different Git branches.
  • Log formatting — add severity-level colors to structured log output in long-running processes to make scanning logs faster.
  • Test suite output — color pass/fail indicators to make failing tests immediately visible in CI terminal output.
  • Interactive CLI menus — bold selected items and dim inactive options in interactive prompts written in Python or Node.

Frequently asked questions

Why does my ANSI color not work in some terminals?

Older terminals (Windows cmd.exe pre-2016, some minimal Linux terminals) do not support ANSI codes. On modern Windows 10+, enable virtual terminal processing or use Windows Terminal. In scripts, check for terminal support via the TERM environment variable or the NO_COLOR convention.

What is the difference between \033[, \e[ and \x1b[?

These are all the same ESC character (ASCII 27) written in different notations: \033 is octal, \x1b is hex, and \e is a shorthand accepted by bash and some other shells. In Python, use \x1b or the chr(27) approach. In JavaScript, \x1b works in string literals.

How do I output colors with echo?

Use echo -e "\033[31mRed text\033[0m" on bash (the -e flag enables escape interpretation). On zsh, echo interprets escapes by default. Alternatively, use printf "\033[31m%s\033[0m\n" "Red text" which is portable across shells.

What is the NO_COLOR convention?

The NO_COLOR environment variable (no-color.org) signals that the user prefers no color output. CLI tools should check: if [ -n "$NO_COLOR" ]; then disable all ANSI codes. This respects accessibility needs and environments where ANSI codes appear as literal characters.

How do I get 256 or truecolor (24-bit) colors?

For 256-color: \033[38;5;Nm for foreground (N = 0-255) and \033[48;5;Nm for background. For 24-bit truecolor: \033[38;2;R;G;Bm for foreground. Check terminal support with printf "\033[38;2;255;100;0mtruecolor\033[0m\n".