OSC escape codes — window title, hyperlinks, colour palette
Operating System Command. The `ESC ] n ; payload BEL` (or `ESC \` terminated) family — window / icon title, the OSC 8 hyperlink protocol, dynamic colour queries, palette overrides, working-directory reporting. The protocol layer that lets terminals talk back to and configure the emulator itself.
20 sequences
OSC cookbook — driving the terminal chrome with six recipes
OSC sits one layer outside CSI — instead of painting glyphs in the grid, OSC drives the terminal's surrounding chrome: window title, hyperlinks, the colour palette itself, shell-integration markers, inline images, progress bars. Six stops cover the envelope syntax, window/tab titles as the canonical example, the OSC 8 hyperlink protocol, palette overrides, the FinalTerm OSC 133 prompt marks that turn a flat byte stream into structured blocks, and the OSC 1337 + OSC 9;4 codes that talk to the host OS's taskbar.
1. The envelope —
\x1b]…\x07or\x1b\\Every OSC sequence is
\x1b](ESC]), a numericPs(0,8,133, etc.), then;-separated payload, then a terminator. Two terminators are both valid:\x07(BEL — xterm's original de-facto choice, one byte) and\x1b\\(ST — ECMA-48 standards-correct, two bytes speltESC \). Modern emulators accept both; reach for BEL on a one-shot shellprintf, reach for ST when you're nesting OSC inside DCS or tmux pass-through where BEL would mean something else. There's also an 8-bit C1 form\x9d … \x9cyou'll see in old VT manuals but never in the wild. **Always emit the terminator** — a missing BEL/ST hangs the parser until the next byte that happens to be one, so the next stray^Gin your output silently closes your OSC and treats everything past it as text.2. Window & tab titles —
OSC 0 / 1 / 2The canonical OSC sequence:
\x1b]0;TITLE\x07sets both the window title bar and the icon name (taskbar text on Windows, dock-icon hover text on macOS). Sub-codes split the two:OSC 1icon name only,OSC 2window title only — useful when your shell wants the window title to stay pinned to the project while the icon flashes an attention-state. The common shell idiom wires it intoPROMPT_COMMAND/precmd:printf '\033]0;%s — %s\007' "$USER@$HOST" "$(basename "$PWD")"redraws the title every prompt with the current directory. Inside tmux / screen the inner OSC needs the\ePtmux;\eDCS wrapper or it won't reach the outer terminal — tmux'sset-option -g set-titles ondoes the wrapping for you. Supported everywhere except the bare Linux console (no chrome to set).3. OSC 8 hyperlinks —
\x1b]8;;URI\x07TEXT\x1b]8;;\x07Specified by gnome-terminal in 2017 and now broadly adopted — iTerm2, kitty, wezterm, ghostty, Windows Terminal, konsole, gnome-terminal 3.26+. Two-part envelope: the open sets a URI, the close (empty URI) ends the run. Anything between (further SGR colour / weight included) becomes the clickable text. Optional
id=<token>between the two;s lets the emulator de-duplicate a multi-line link so hovering one cell highlights every matching cell — but **never re-use the sameidfor two different URIs in the same session**: pre-21.04 Konsole and pre-3.36 gnome-terminal key dedup on id alone and will silently rewrite the second URL to the first. Plain HTTPS works everywhere;file://only on the emulators that own a renderer for it. Unsupported terminals (Linux console, macOS Terminal, alacritty) just print the text without the link — safe to emit unconditionally.See alsoOSC 8 — Inline hyperlink4. Palette overrides —
OSC 4/10/11/104Four codes let you repaint the palette itself rather than picking from it.
\x1b]4;n;rgb:RR/GG/BB\x1b\\overrides slotn(0–255) of the xterm 256-palette for this session — so\e]4;1;rgb:e0/3a/3a\e\\retunes the red that\x1b[31mwill use from then on.OSC 10andOSC 11change the default foreground and default background colours independent of the indexed palette — handy for a TUI to dim the chrome behind a modal without touching any text colour. The matching reset is\x1b]104;n\x1b\\(revert one slot to compiled-in default) or bare\x1b]104\x1b\\to revert every slot at once;OSC 110/OSC 111(no args) reset default fg / bg respectively. The colour string accepts the X11rgb:RRRR/GGGG/BBBBlong form too, plus#rrggbbon most emulators.5. Shell prompt marks —
OSC 133(FinalTerm A / B / C / D)FinalTerm's OSC 133 turns a shell session from a flat byte stream into structured blocks the terminal can navigate. Four codes mark boundaries:
\x1b]133;A\x07before the prompt,\x1b]133;B\x07between prompt and user input,\x1b]133;C\x07between Enter and the command's stdout,\x1b]133;D;<exit>\x07after the command exits (exit code optional). Kitty, iTerm2, WezTerm, Ghostty, Warp, and VS Code's terminal then expose UX you can't fake otherwise: jump to previous / next prompt with a keystroke, fold a single command's output, decorate the gutter with red-triangle / green-tick based on exit code, scroll one command at a time. Wire it into yourPS1+preexec+precmdhooks (atuin,starship, and the emulator-bundled scripts do it for you). Pair with\x1b]7;file://host/path\x1b\\(OSC 7, cwd advertising) so the terminal also knows where each block ran — together they're the full shell-integration story.6. Inline images & progress —
OSC 1337andOSC 9 ; 4Two out-of-band OSC codes drive the host OS's window chrome rather than painting glyphs. **
\x1b]1337;File=name=<b64>;inline=1:<b64-payload>\x07** is iTerm2's inline-image protocol — the same vocabulary covers PNG, JPEG, animated GIF, PDF — and is the path theimgcatshell script uses (iTerm2 ships it; WezTerm and Konsole 22.04+ implement theFile=subset). The sameOSC 1337envelope also carries non-File subcodes (SetUserVar,SetMark,ReportCellSize) for status-bar variables, navigation marks, and pixel-accurate Sixel sizing. **\x1b]9;4;<state>;<pct>\x07** is the ConEmu progress protocol Windows Terminal 1.18+ standardised —state=1normal,2error,3indeterminate,4paused,0clear — and the terminal forwards it to the OS taskbar (WT) or tab indicator (Ghostty, WezTerm). Don't confuse it with iTerm2'sOSC 9 ; <message>notification: the;4second token is the disambiguator, so always emit the full shape. Both are safe to emit unconditionally — unsupported emulators silently drop the bytes.
All sequences in this family
- OSC 0 / 2 — Set window/icon title
\x1b]0;TITLE\x07Change the terminal window's title bar text.
- OSC 8 — Inline hyperlink
\x1b]8;;URI\x07TEXT\x1b]8;;\x07Render clickable hyperlinks in terminal output (gnome-terminal 3.26+, iTerm2, Windows Terminal, kitty, ...).
- OSC 10 / 11 — Set default foreground / background color
\x1b]10;#RRGGBB\x07 (set fg) \x1b]11;#RRGGBB\x07 (set bg)Set the terminal's default foreground (OSC 10) or background (OSC 11) color, or query the current value.
- OSC 52 — System clipboard read/write
\x1b]52;c;BASE64\x07 (set clipboard 'c' to BASE64 decoded bytes)Set (or query) the host system clipboard via the terminal — works over SSH without X11 forwarding.
- OSC 4 — Set palette colour (and query)
\x1b]4;N;#RRGGBB\x07 (set) \x1b]4;N;?\x07 (query)Change one of the 256 palette colours, or query its current value.
- OSC 9 — Toast notification (iTerm2 / Windows Terminal)
\x1b]9;MESSAGE\x07Trigger a native OS desktop notification from the terminal — long-running job done, build finished, etc.
- OSC 7 — Current working directory hint
\x1b]7;file://HOST/PATH\x07Tell the terminal which directory the shell is in — used for tab labels, 'new tab here', and remote-aware split-pane workflows.
- OSC 110 / 111 / 112 — Reset default fg / bg / cursor colour
\x1b]110\x07 (reset fg) \x1b]111\x07 (reset bg) \x1b]112\x07 (reset cursor)Restore the terminal's user-default foreground, background, or cursor colour after OSC 10 / 11 / 12 overrode it.
- OSC 12 — Set cursor colour (and query)
\x1b]12;#RRGGBB\x07 (set) \x1b]12;?\x07 (query)Override the terminal's cursor colour, or query the current value via the OSC reply.
- OSC 17 — Set highlight (selection) background colour
\x1b]17;#RRGGBB\x07 (set) \x1b]17;?\x07 (query)Change the background colour the terminal paints behind selected (highlighted) text.
- OSC 19 — Set highlight (selection) foreground colour
\x1b]19;#RRGGBB\x07 (set) \x1b]19;?\x07 (query)Change the foreground colour the terminal uses for selected (highlighted) text.
- OSC 104 — Reset palette colour (one index or all)
\x1b]104;N\x07 (reset index N) \x1b]104\x07 (reset all)Restore one palette index — or the whole 256-colour palette — to the terminal's user-configured defaults.
- OSC 1 — Set icon name (separate from window title)
\x1b]1;ICON-NAME\x07Set the X11-style icon name independent of the visible window title — historically the label shown when the window is minimised.
- OSC 22 — Set X11 mouse pointer / cursor name
\x1b]22;CURSOR-NAME\x07Override the mouse-pointer glyph the terminal window draws — historically xterm-specific, exposed via standard X cursor names.
- OSC 50 — Set or query font (xterm font selector)
\x1b]50;FONT-SPEC\x07 (set) \x1b]50;?\x07 (query)Switch the terminal's display font at runtime, or query the current font — xterm extension, also implemented by a handful of forks.
- OSC 133 — Semantic prompt marks (FinalTerm A / B / C / D)
\x1b]133;A\x07 (prompt start) \x1b]133;B\x07 (command start) \x1b]133;C\x07 (output start) \x1b]133;D;<exit>\x07 (command end)Tag shell prompt-start / command-start / output-start / command-end so the terminal can jump between prompts, fold output, and decorate exit codes.
- OSC 1337 — iTerm2 inline images & file transfer
\x1b]1337;File=name=<base64>;size=<bytes>;inline=1:<base64-data>\x07Embed images, files, or proprietary iTerm2 state into the terminal stream — the predecessor protocol that Kitty / Sixel later largely supplanted.
- OSC 9 ; 4 — ConEmu progress indicator (Windows Terminal / Ghostty)
\x1b]9;4;<state>;<percent>\x07Push live progress percentages / paused / error states to the taskbar or tab icon — the ConEmu protocol that Windows Terminal 1.18+ standardised.
- OSC 10 / 11 / 12 query — Detect default fg / bg / cursor color (dark vs light)
\x1b]10;?\x07 (fg) \x1b]11;?\x07 (bg) \x1b]12;?\x07 (cursor)Ask the terminal for its current default foreground (10) / background (11) / cursor (12) color — the canonical way to auto-pick a dark or light theme.
- OSC 4 query — Read 256-color palette index (`\x1b]4;<n>;?\x07`)
\x1b]4;<n>;?\x07Ask the terminal what RGB value is currently bound to palette index `n` (0–255) — used by theme inspectors, screenshot tools, and palette-migration scripts.