Skip to main content
ansicode
SGR (color & attributes)

SGR escape codes — color and text attributes

Select Graphic Rendition. The `ESC [ … m` family — every code that paints colour, bold, italic, underline, blink, reverse, strikethrough, or any other text-style attribute onto the terminal. The largest and most-googled corner of the ANSI alphabet.

24 sequences

Colour cookbook — reading the family in six stops

The SGR family is the largest corner of ANSI — every code that paints colour or style onto a glyph. Six stops, top to bottom, get you the whole picture: how to close a run, the original 16, the 256-indexed palette, 24-bit truecolor, the OSC trick for repainting a slot, and the colon-form sub-params standards-pedants prefer.

  1. 1. One-shot reset — \x1b[0m

    Every coloured string ends with \x1b[0m. It zeros out fg + bg + every attribute the previous SGR opened — bold, italic, underline, the lot. Omit it and the colour bleeds into the next prompt line, the shell's PS1, the file you piped output into, even the kernel ringbuffer if you're unlucky. Treat it as the closing brace of every coloured run. \x1b[m (empty params) means the same thing per ECMA-48, but \x1b[0m is what nearly every CLI in the wild emits.

  2. 2. 16-color basic — \x1b[31m and friends

    The original ECMA-48 palette: 30–37 picks the eight foreground colours (black, red, green, yellow, blue, magenta, cyan, white), 40–47 picks the matching background. Prepend 1; for bold — on many terminals bold also brightens the colour, which is why \x1b[1;31m reads redder than \x1b[31m. The bright slots are exposed explicitly via 90–97 (fg) and 100–107 (bg) — eight more colours, no bold required. Codes 39 and 49 are the matching defaults: default fg and default bg, useful when you want to clear colour but keep an active attribute like bold or italic going.

  3. 3. 256-color indexed — \x1b[38;5;n m

    The xterm-256color palette: slots 0–15 mirror the 16 basic / bright colours; 16–231 is a 6×6×6 RGB cube (the levels are 0, 95, 135, 175, 215, 255 — not a linear 0–255 ramp); 232–255 is 24 shades of grey from near-black to near-white. To pick orange-ish: \x1b[38;5;208m. To pick mid-grey: \x1b[38;5;244m. 38 is foreground, 48 is background, the rest of the form is identical. Almost every terminal that's not a 1995-era console supports this — including ssh hops through screen or tmux, provided the inner $TERM is one of xterm-256color, screen-256color, tmux-256color.

  4. 4. Truecolor — \x1b[38;2;r;g;b m

    24-bit RGB when 256 isn't enough. \x1b[38;2;255;128;0m paints a precise #FF8000 orange; \x1b[48;2;30;30;30m sets a near-black background. Modern emulators (kitty, alacritty, wezterm, iTerm2, Windows Terminal, gnome-terminal ≥ 3.20, konsole, ghostty) all support this. The way to detect support at runtime is the $COLORTERM env var — when it reads truecolor or 24bit, you're safe to emit RGB; when it's empty, fall back to the closest 256-indexed slot. $TERM alone is unreliable for this: xterm-256color doesn't imply truecolor capability, and many terminals set $COLORTERM even when $TERM is plain xterm.

  5. 5. OSC palette override — repaint the slot itself

    Strictly speaking this is OSC, not SGR — but readers asking "how do I make my red look better?" land here. OSC 4 ; n ; rgb:RR/GG/BB ST overrides palette slot n (0–255) for the current session. So \e]4;1;rgb:e0/3a/3a\e\\ re-tunes the red that \x1b[31m uses. Pair with OSC 10 for the default foreground colour and OSC 11 for the default background; use OSC 104 ; n ST to reset slot n back to the compiled-in default, or bare OSC 104 ST to reset every slot. Useful for theming a TUI without forcing users to edit their terminal config.

  6. 6. SGR colon sub-params — the standards-correct form

    ECMA-48 actually specifies the sub-parameter separator as colon (:), not semicolon — 38:2::r:g:b is the standards-correct truecolor form (the empty slot between 2 and r is the colour-space ID, usually omitted). Most terminals accept BOTH ; and : because xterm did, but the colon form is what wezterm, kitty, foot, and the Linux kernel console expect for newer SGR extensions like underline-colour. The 21st-century example: to draw red wavy underline beneath default-colour text you write \x1b[4:3m\x1b[58:2::255:0:0m. The 4:3 opens curly underline, 58:2::r:g:b sets the underline colour distinct from the foreground. Knowing this saves a head-scratch the next time a parser surprises you.

All sequences in this family

Browse other families