Escape Sequence Cheat Sheet

A single-page reference. Every row includes a shell command that generates it, so you can watch a real terminal do it.

CSI = ESC [ = 1b 5b · OSC = ESC ] = 1b 5d · SS3 = ESC O = 1b 4f · ST = ESC \ = 1b 5c · DCS = ESC P = 1b 50

A missing parameter means the sequence's default, which is usually 1 — never 0.


C0 Controls

ByteNameEffectGenerate
07BELBell; also terminates OSCprintf '\a'
08BSCursor left 1. Does not erase.printf 'abc\b \b'
09HTNext tab stop; does not wrapprintf 'a\tb\n'
0aLFDown one row; scrolls at the region bottomprintf 'a\nb\n'
0b 0cVT FFTreated as LFprintf 'a\vb\n'
0dCRColumn 0printf 'aaa\rb\n'
0e 0fSO SIInvoke G1 / G0 charsetprintf '\016lqk\017\n'
1bESCIntroduces a sequence; aborts any in progress—

Cursor Movement

SequenceNameEffectGenerate
CSI Ps ACUUUp Ps (default 1); clamps; no scrollprintf '\n\n\033[2Aup\n'
CSI Ps BCUDDown Psprintf '\033[2Bdown\n'
CSI Ps CCUFRight Ps; clamps at the last columnprintf 'a\033[10Cb\n'
CSI Ps DCUBLeft Psprintf 'abc\033[2Dxy\n'
CSI Ps ECNLDown Ps and column 0printf 'a\033[2Eb\n'
CSI Ps FCPLUp Ps and column 0printf '\n\n\033[2Fb\n'
CSI Ps GCHATo column Ps (1-based)printf 'abc\033[1Gx\n'
CSI Ps dVPATo row Ps, column unchangedprintf '\033[5dx\n'
CSI Pr;Pc HCUPTo row Pr, column Pc (1-based)printf '\033[3;10Hhere\n'
CSI Pr;Pc fHVPIdentical to CUPprintf '\033[3;10fhere\n'
CSI Ps ICHTForward Ps tab stopsprintf 'a\033[2Ib\n'
CSI Ps ZCBTBack Ps tab stopsprintf 'a\t\t\033[1Zb\n'
ESC 7DECSCSave cursor + style + origin modeprintf '\0337'
ESC 8DECRCRestore all of itprintf '\0338'
CSI s / CSI uSCOSC/SCORCSave/restore position onlyprintf '\033[s'

All cursor movement clears the pending-wrap flag.


Erase

SequenceEffectGenerate
CSI 0 J (default)Cursor to end of screenprintf '\033[J'
CSI 1 JStart of screen to cursorprintf '\033[1J'
CSI 2 JEntire screen; cursor unmovedprintf '\033[2J\033[H'
CSI 3 JErase scrollback (xterm ext.)printf '\033[3J'
CSI 0 K (default)Cursor to end of lineprintf 'abcdef\033[3D\033[K\n'
CSI 1 KStart of line to cursorprintf 'abcdef\033[3D\033[1K\n'
CSI 2 KEntire lineprintf 'abcdef\033[2K\n'
CSI Ps XECH: erase Ps chars in place, cursor unmovedprintf 'abcdef\033[1;3H\033[2X\n'

Erased cells take the current SGR background — printf '\033[41m\033[2J' gives a red screen.


Insert / Delete / Scroll

SequenceEffectGenerate
CSI Ps @ICH: insert Ps blanks; the line shifts rightprintf 'abcdef\033[1;3H\033[2@\n'
CSI Ps PDCH: delete Ps chars; the line shifts leftprintf 'abcdef\033[1;3H\033[2P\n'
CSI Ps LIL: insert Ps lines within the regionprintf '\033[2L'
CSI Ps MDL: delete Ps lines within the regionprintf '\033[2M'
CSI Ps SSU: scroll region up Ps; cursor unmovedprintf '\033[3S'
CSI Ps TSD: scroll region down Psprintf '\033[3T'
CSI Ps bREP: repeat the last printed char Ps timesprintf 'x\033[10b\n'
ESC DIND: down, scroll at the region bottomprintf '\033D'
ESC MRI: up, scroll down at the region topprintf '\033M'
ESC ENEL: CR + LFprintf '\033E'

IL/DL/SU/SD operate within the scroll region only; IL/DL are no-ops outside it.


SGR (Attributes and Color)

PsEffectPsEffect
0Reset all22Normal intensity (clears bold and dim)
1Bold23Not italic
2Dim24Not underlined (all styles)
3Italic25Not blinking
4Underline (4:1…4:5 for styles)27Not inverse
5Blink28Not hidden
7Inverse29Not struck
8Hidden53 / 55Overline / not
9Strikethrough21Double underline
30–37Foreground, colors 0–740–47Background, colors 0–7
90–97Foreground, bright 8–15100–107Background, bright 8–15
39Default foreground49Default background
38;5;nForeground, palette n48;5;nBackground, palette n
38;2;r;g;bForeground, direct RGB48;2;r;g;bBackground, direct RGB
38:2::r:g:bThe same, colon form (the standard)58;5;nUnderline color
printf '\033[1;31mbold red\033[0m\n'
printf '\033[38;5;196mpalette 196\033[0m\n'
printf '\033[38;2;255;100;0mtruecolor\033[0m\n'
printf '\033[4:3;58;5;196mcurly red underline\033[0m\n'
for i in $(seq 0 255); do printf "\033[48;5;%dm %3d \033[0m" $i $i
  [ $(( (i+1) % 16 )) -eq 0 ] && echo; done

Palette layout: 0–7 standard · 8–15 bright · 16–231 a 6×6×6 cube (16 + 36r + 6g + b) · 232–255 grayscale.


DEC Private Modes (CSI ? Ps h set · CSI ? Ps l reset)

PsNameSet meansGenerate
1DECCKMArrows send SS3 not CSIprintf '\033[?1h'
5DECSCNMReverse video, whole screenprintf '\033[?5h'; sleep 1; printf '\033[?5l'
6DECOMOrigin mode: positioning is region-relativeprintf '\033[?6h'
7DECAWMAutowrap (default on)printf '\033[?7l'
12—Cursor blinkprintf '\033[?12h'
25DECTCEMCursor visible (default on)printf '\033[?25l'; sleep 1; printf '\033[?25h'
47 / 1047—Alternate screen (legacy variants)printf '\033[?47h'
1048—Save/restore cursorprintf '\033[?1048h'
1049—Alt screen + save cursor + clearprintf '\033[?1049h'; sleep 1; printf '\033[?1049l'
1000—Mouse: press/releaseprintf '\033[?1000h\033[?1006h'
1002—Mouse: + dragprintf '\033[?1002h'
1003—Mouse: + all motionprintf '\033[?1003h'
1004—Focus reporting (CSI I / CSI O)printf '\033[?1004h'
1006—Mouse: SGR encodingprintf '\033[?1006h'
2004—Bracketed pasteprintf '\033[?2004h'
2026—Synchronized outputprintf '\033[?2026h'
2027—Grapheme clusteringprintf '\033[?2027h'

ANSI modes (no ?): 4 IRM (insert/replace) · 20 LNM (LF also does CR).

Always reset mouse and alt-screen modes on exit, on every path including panic. Leaving them on makes the user's shell emit garbage on every click.


Scroll Region and Reset

SequenceEffectGenerate
CSI Pt;Pb rDECSTBM: margins (1-based, inclusive). Homes the cursor.printf '\033[5;20r'
CSI rReset to the full screenprintf '\033[r'
CSI ! pDECSTR: soft reset (modes, margins, SGR; keeps content)printf '\033[!p'
ESC cRIS: hard reset (both screens, scrollback, modes, title)printf '\033c'
ESC # 8DECALN: fill the screen with Eprintf '\033#8'

Tabs

SequenceEffectGenerate
ESC HHTS: set a tab stop at the cursorprintf '\033H'
CSI 0 gTBC: clear the stop at the cursorprintf '\033[g'
CSI 3 gClear all stopsprintf '\033[3g'

Default: every 8 columns.


Reports and Queries (the terminal replies)

SequenceReplyGenerate
CSI 5 nDSR: CSI 0 n ("OK")printf '\033[5n'
CSI 6 nCPR: CSI row ; col R (1-based)printf '\033[6n'; read -r -d R p; echo "${p#*[}"
CSI cDA1: e.g. CSI ? 6 2 ; 2 2 cprintf '\033[c'
CSI > cDA2: version infoprintf '\033[>c'
CSI ? Ps $ pDECRQM: CSI ? Ps ; Pv $ yprintf '\033[?1049$p'
OSC 11 ; ? STBackground colorprintf '\033]11;?\033\\'
OSC 4 ; n ; ? STPalette entry nprintf '\033]4;1;?\033\\'

CSI 6n is the best probe for differential testing: it makes the terminal report its internal state machine-readably.


OSC (String Sequences)

Terminated by BEL (07) or ST (ESC \).

PsEffectGenerate
0Set window title and icon nameprintf '\033]0;My Title\007'
1Icon name onlyprintf '\033]1;icon\007'
2Window title onlyprintf '\033]2;title\007'
4Set/query palette colorprintf '\033]4;1;#ff0000\033\\'
7Report the working directoryprintf '\033]7;file://localhost/tmp\033\\'
8Hyperlinkprintf '\033]8;;https://example.com\033\\link\033]8;;\033\\\n'
10/11/12Default fg / bg / cursor colorprintf '\033]11;#001122\033\\'
52Clipboard (write; reads denied by default)printf '\033]52;c;%s\033\\' "$(echo hi | base64)"
104Reset paletteprintf '\033]104\033\\'
110/111/112Reset fg / bg / cursor colorprintf '\033]111\033\\'
133Semantic prompt marks (A/B/C/D)PS1='\[\e]133;A\e\\\]$ '

Charsets

SequenceEffectGenerate
ESC ( BG0 = ASCIIprintf '\033(B'
ESC ( 0G0 = DEC Special Graphics (box drawing)printf '\033(0lqqqk\033(B\n'
SO (0e) / SI (0f)Invoke G1 / G0printf '\016lqk\017\n'

DEC Special Graphics maps lqkxjmtuvwn to ┌─┐│┘└├┤┬┴┼. If a program's box drawing shows as lqqqk, this is the missing feature.


Quick Diagnostic Recipes

# Where is the cursor, really?
cursor() { printf '\033[6n'; read -r -d R p; echo "${p#*[}"; }

# Does this terminal do truecolor, or is it quantizing?
awk 'BEGIN{for(i=0;i<256;i++)printf "\033[48;2;%d;0;0m ",i; print "\033[0m"}'
#   smooth = truecolor;  banded = 256-color

# What sequences does a program actually emit?
script -q -c 'vim -u NONE -c q' /dev/null | cat -v | head -40

# Recover a terminal wrecked by a crashed TUI:
printf '\033c'        # RIS, hard reset
reset                 # or this
stty sane             # if input is broken (may need Ctrl+J instead of Enter)

# Turn OFF everything a crashed program may have left on:
printf '\033[?1000l\033[?1002l\033[?1003l\033[?1006l\033[?2004l\033[?1004l\033[?25h\033[?1049l\033[0m'

See also: Glossary · termios Cheat Sheet · Key Encoding Table · The CSI Catalog