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. One-shot reset —
\x1b[0mEvery 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'sPS1, 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[0mis what nearly every CLI in the wild emits.See alsoSGR 0 — Reset / Normal2. 16-color basic —
\x1b[31mand friendsThe 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;31mreads 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 fganddefault bg, useful when you want to clear colour but keep an active attribute likeboldoritalicgoing.3. 256-color indexed —
\x1b[38;5;n mThe 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.38is foreground,48is background, the rest of the form is identical. Almost every terminal that's not a 1995-era console supports this — including ssh hops throughscreenortmux, provided the inner$TERMis one ofxterm-256color,screen-256color,tmux-256color.4. Truecolor —
\x1b[38;2;r;g;b m24-bit RGB when 256 isn't enough.
\x1b[38;2;255;128;0mpaints a precise#FF8000orange;\x1b[48;2;30;30;30msets 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$COLORTERMenv var — when it readstruecoloror24bit, you're safe to emit RGB; when it's empty, fall back to the closest 256-indexed slot.$TERMalone is unreliable for this:xterm-256colordoesn't imply truecolor capability, and many terminals set$COLORTERMeven when$TERMis plainxterm.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 SToverrides palette slotn(0–255) for the current session. So\e]4;1;rgb:e0/3a/3a\e\\re-tunes the red that\x1b[31muses. Pair withOSC 10for the default foreground colour andOSC 11for the default background; useOSC 104 ; n STto reset slotnback to the compiled-in default, or bareOSC 104 STto reset every slot. Useful for theming a TUI without forcing users to edit their terminal config.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:bis the standards-correct truecolor form (the empty slot between2andris 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. The4:3opens curly underline,58:2::r:g:bsets 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
- SGR 0 — Reset / Normal
\x1b[0mClear all text attributes and colors back to the terminal default.
- SGR 1 — Bold / Increased intensity
\x1b[1mRender following text in bold (or bright on some terminals).
- SGR 2 — Dim / Faint
\x1b[2mRender following text at reduced intensity (faint).
- SGR 5 — Blink (slow)
\x1b[5mBlink the following text (≤ 150 blinks/minute).
- SGR 8 — Conceal / Hidden
\x1b[8mRender following text invisibly (cursor still advances).
- SGR 9 — Strikethrough
\x1b[9mRender following text with a horizontal line through it.
- SGR 39 — Default foreground color
\x1b[39mReset only the foreground color (leaves attributes + bg intact).
- SGR 49 — Default background color
\x1b[49mReset only the background color (leaves attributes + fg intact).
- SGR 48;5;n — 256-color background
\x1b[48;5;NmPick a background color from the 256-color xterm palette.
- SGR 48;2;R;G;B — 24-bit truecolor background
\x1b[48;2;R;G;BmPick any of 16,777,216 background RGB colors directly.
- SGR 3 — Italic
\x1b[3mRender following text in italic; not universally supported.
- SGR 4 — Underline
\x1b[4mRender following text with an underline.
- SGR 7 — Reverse video
\x1b[7mSwap foreground and background colors.
- SGR 30–37 — Foreground color (8 basic)
\x1b[31m (red, similarly 30–37)Set foreground to one of black/red/green/yellow/blue/magenta/cyan/white.
- SGR 40–47 — Background color (8 basic)
\x1b[41m (red bg, 40–47)Set background to one of black/red/green/yellow/blue/magenta/cyan/white.
- SGR 90–97 — Bright foreground color
\x1b[91m (bright red, 90–97)Bright variants of the 8 basic foreground colors (aixterm/xterm extension).
- SGR 100–107 — Bright background color
\x1b[101m (bright red bg, 100–107)Bright variants of the 8 basic background colors.
- SGR 38;5;n — 256-color foreground
\x1b[38;5;NmPick a foreground color from the 256-color xterm palette.
- SGR 38;2;R;G;B — 24-bit truecolor foreground
\x1b[38;2;R;G;BmPick any of 16,777,216 foreground RGB colors directly.
- SGR 21 — Doubly underlined
\x1b[21mRender text with a double underline — distinct from single underline (SGR 4).
- SGR 53 — Overlined
\x1b[53mRender text with a line drawn ABOVE the glyphs — mirror of underline. Disable with SGR 55.
- SGR 26 — Proportional spacing (ECMA-48; ignored by every common terminal)
\x1b[26mDefined by ECMA-48 as 'proportional spacing on' — but no common terminal emulator implements it. Document the gotcha.
- SGR colon sub-parameters — ITU-T T.416 alternative (38:2:: / 48:2:: / 38:5:)
\x1b[38:2::255:128:64m (T.416) \x1b[38;2;255;128;64m (xterm-legacy)Use `:` (0x3a) instead of `;` (0x3b) between extended-color sub-parameters — the spec-blessed form that disambiguates compound SGR codes from independent ones.
- SGR 73 / 74 / 75 — Superscript / Subscript / Neither
\x1b[73m superscript \x1b[74m subscript \x1b[75m neitherMintty's superscript / subscript SGR pair, now shipped by Kitty 0.32+, WezTerm, foot, and iTerm2. The first SGR attribute that lets terminal output typeset math, footnotes, and chemical formulae without trick characters.