Skip to main content
ansicode

SGR 48;5;n — 256-color background

Pick a background color from the 256-color xterm palette.

Byte forms

Every common string-literal form so you can paste-and-search either direction.

\\x1b[\x1b[48;5;Nm
\\033[\033[48;5;Nm
\\e[\e[48;5;Nm
ESC [ESC [ 4 8 ; 5 ; N m
hex1b 5b 34 38 3b 35 3b <N> 6d

Live preview

Rendered in your browser via the same tokeniser the decoder uses — no terminal needed.

196 / 208 / 226 / 46 / 51 / 201

Description

Background sibling of sgr-fg-256 (\x1b[38;5;Nm). N is 0–255 with identical palette layout — 0–7 the basic 16-color slots (the 40–47 equivalents), 8–15 the bright slots (the 100–107 equivalents), 16–231 a 6×6×6 RGB cube indexed as 16 + 36*r + 6*g + b with each component in 0..5, and 232–255 a 24-step grayscale ramp from near-black to near-white. The colon-form \x1b[48:5:Nm is accepted by ECMA-48-strict parsers alongside the more common semicolon form. The 256-color extension originated in xterm and is documented in xterm-ctlseqs §"256-color support"; it predates the truecolor escape by ~15 years and remains the most portable way to express anything beyond the basic 16.

Terminal support is broader than truecolor but narrower than the 16-color baseline. The Linux console / fbcon refuses 256-color in framebuffer mode (the kernel palette only holds 16 slots), so \x1b[48;5;202m renders as a no-op or fallback color depending on the kernel build — the support matrix above marks it no. Windows conhost.exe supports 256 only since Windows 10 1809; older builds quantize to the nearest 16-color slot. The 16–231 cube is universally available on every other emulator in the matrix; the 232–255 grayscale ramp is where emulators diverge most — alacritty, kitty, wezterm and modern gnome-terminal render an even ramp, while screen 4.x quantizes the dimmer grays to pure black on some builds and 5.0 fixed it. tmux requires set -ag terminal-overrides ',xterm-256color:Tc' (or the modern RGB cap) to forward 256-color escapes intact to the outer terminal; without it \x1b[48;5;202m reaches tmux but never makes it past.

Reset just the background with \x1b[49m — surgical, leaves foreground and other attributes intact. The full reset \x1b[0m clears everything. To convert an arbitrary RGB triplet to the nearest cube index, use 16 + 36*round(r*5/255) + 6*round(g*5/255) + round(b*5/255); for grayscale targets use 232 + round(luma*23/255). If the rendered output looks wrong on a specific terminal, probe $COLORTERM — when it is truecolor or 24bit prefer sgr-bg-truecolor for exact colors; when it is unset or empty but $TERM matches *-256color, the 256-cube is the most portable target.

Spec citation: xterm-ctlseqs (256-color extension)

Examples

bash
printf '\033[48;5;202m highlight \033[49m\n'
python
print('\x1b[48;5;202m highlight \x1b[0m')
go
fmt.Print("\x1b[48;5;202m highlight \x1b[0m")
javascript
console.log('\x1b[48;5;202m highlight \x1b[0m')
c
printf("\x1b[48;5;202m highlight \x1b[0m\n");

Used in

Real-world tools that emit this sequence — anchors the bytes to commands you've already used.

  • batsyntax-theme line-highlight background (active line gutter)
  • delta256-palette diff backgrounds — soft pastels for added/removed hunks
  • vim, neovim colourschemes (Solarized, Gruvbox) on 256-colour terminals
  • tig (git TUI) — selected line bg
  • fzf default --color=bg+:236 dark grey on the active row

Frequently asked

Short answers to the questions developers actually search for this sequence.

Why does \x1b[48;5;0m not equal \x1b[40m on iTerm2?
Both are nominally slot 0, but they go through different palette tables. iTerm2's per-profile colour preset (e.g. Solarized Dark, Tango) remaps slots 0–15 of the 256-colour palette independently from the legacy 8-colour \x1b[40m/SGR-40 table. xterm and most other emulators tie the two tables together for slots 0–7 + their bright pair, so \x1b[40m == \x1b[48;5;0m there. To get bitwise-stable colour across emulators use truecolor (\x1b[48;2;r;g;bm); to target the legacy table specifically, stick with \x1b[40m\x1b[47m.
Why does my code ignore the 5; and run \x1b[48;Nm instead — what does the terminal do?
ECMA-48 reserves the leading sub-parameter (5 = indexed / 2 = direct RGB) precisely so a single parameter following SGR 48 has no defined meaning. Most terminals either ignore the whole 48;N clause as malformed or treat N as if it were the next top-level parameter (so \x1b[48;5m becomes \x1b[48m;\x1b[5m and your text starts blinking). Always write the prefix: \x1b[48;5;Nm for palette, \x1b[48;2;r;g;bm for truecolor — and lint your codebase to catch the missing 5;.

Terminal support

xterm
yes
Linux console (fbcon)
no
macOS Terminal.app
yes
iTerm2
yes
Windows Terminal
yes
cmd.exe / ConPTY
partial
kitty
yes
alacritty
yes
WezTerm
yes
Ghostty
yes
GNOME Terminal
yes
Konsole
yes
tmux
yes
GNU screen
yes

Related sequences

In the family cookbook

SGR cookbook · 3. 256-color indexed — `\x1b[38;5;n m`