Skip to main content
ansicode

OSC 4 — Set palette colour (and query)

Change one of the 256 palette colours, or query its current value.

Byte forms

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

\\x1b[\x1b]4;N;#RRGGBB\x07 (set) \x1b]4;N;?\x07 (query)
\\033[\033]4;N;...\007
\\e[\e]4;N;...\a
ESC [ESC ] 4 ; N ; COLOR BEL
hex1b 5d 34 3b <N> 3b ... 07

Description

Set the colour at index `N` (0..255) in the terminal's working palette. Indexes 0..7 are the basic colours (black / red / green / yellow / blue / magenta / cyan / white), 8..15 the bright variants, 16..231 the 6×6×6 colour cube, 232..255 the 24-step grayscale ramp. Colour syntax matches OSC 10/11: `#RRGGBB`, `#RGB`, `rgb:RRRR/GGGG/BBBB`, or X11 names. Replacing the colour with `?` (`\x1b]4;N;?\x07`) makes the terminal reply with the current value — base16-themes, pywal, and theme-detectors all use this query/set loop to harvest and rewrite the palette at startup. Reset a single index with OSC 104 (`\x1b]104;N\x07`); reset the whole palette with OSC 104 with no parameter.

Spec citation: xterm-ctlseqs (OSC 4)

Examples

bash
printf '\033]4;1;#ff5555\007'   # remap colour 1 (red) to #ff5555\nprintf '\033]4;1;?\007'        # query current colour 1
python
import sys; sys.stdout.write('\x1b]4;1;#ff5555\x07')
go
fmt.Print("\x1b]4;1;#ff5555\x07")
javascript
process.stdout.write('\x1b]4;1;#ff5555\x07')
c
printf("\x1b]4;1;#ff5555\x07");

Terminal support

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

Related sequences

In the family cookbook

SGR cookbook · 5. OSC palette override — repaint the slot itself