Skip to main content
ansicode

SGR 7 — Reverse video

Swap foreground and background colors.

Byte forms

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

\\x1b[\x1b[7m
\\033[\033[7m
\\e[\e[7m
ESC [ESC [ 7 m
hex1b 5b 37 6d

Live preview

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

Reversed colors

Description

ECMA-48 §8.3.117 names SGR 7 "negative image" (also called "reverse video" or "inverse video" in everyday usage). The byte sequence is \x1b[7m. The terminal swaps the currently-active foreground and background colours for subsequent characters — if your prompt has the default fg over default bg, reverse flips to default-bg over default-fg. Critically the swap operates on the *currently active* fg / bg pair, not on the terminal's theme defaults: \x1b[31;43m (red fg on yellow bg) followed by \x1b[7m produces yellow fg on red bg, not theme-default reversed. The matching close is SGR 27 (\x1b[27m), which restores positive image without disturbing the underlying fg / bg state.

SGR 7 has the broadest support of any SGR attribute — every terminal in the matrix above renders it, including Linux console / fbcon and legacy Windows cmd.exe (where SGR 2 / 3 / 4:N / 9 all degrade in various ways). That makes it the safest signal-carrying attribute when you cannot be sure the user is on a modern GUI emulator: rescue prompts, embedded boards, restricted CI logs, screen-5.0-on-Apple sessions all render SGR 7 correctly. The classic application is selection highlighting in less, vim, fzf, htop, and tmux — they all reach for reverse video first because of its universality. The terminfo cap is rev (tput rev / close via tput sgr0 — there is no surgical rmrev in most terminfo entries, though smso / rmso "standout" on some terminals maps to SGR 7 / 27). watch --differences=cumulative uses SGR 7 to flag changed cells; TUI menu libraries (dialog, whiptail, bubbletea's default highlight style) default to reverse for the focused row because mixing in explicit colours risks colliding with the user's theme.

Disable with SGR 27 (\x1b[27m) — surgical, leaves fg / bg / bold / italic / underline / strikethrough untouched. The pitfall to avoid: applying SGR 7 on top of an *unknown* colour pair (e.g. inside a cat of an arbitrary ANSI log) — the result depends on whatever fg / bg was active before, not on a stable reverse-of-theme-defaults. The fix is to emit an explicit \x1b[39;49m (reset both fg + bg to defaults) immediately before SGR 7 when you want predictable theme-relative reverse. For accessibility, reverse video is one of the few attributes that screen readers can convey indirectly (NVDA / JAWS announce "selected" or "highlighted" for cells with reverse set, depending on terminal integration) — but per color-only-signal it should still be paired with explicit text where the meaning matters. Related: sgr-fg-basic / sgr-bg-basic (the explicit colour pair you'd combine with reverse for a theme-stable highlight), sgr-reset (SGR 0 — full reset).

Spec citation: ECMA-48 §8.3.117 (SGR parameter 7)

Examples

bash
printf '\033[7m highlighted \033[0m\n'
python
print('\x1b[7m highlighted \x1b[0m')
go
fmt.Print("\x1b[7m highlighted \x1b[0m")
javascript
console.log('\x1b[7m highlighted \x1b[0m')
c
printf("\x1b[7m highlighted \x1b[0m\n");

Used in

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

  • lesshighlight on the active search-match line
  • fzfselected row in the picker uses reverse video as the cursor indicator
  • vim, neovimCursorLine and Visual highlight groups default to reverse on monochrome terminals
  • man (search highlight on /pattern)
  • htop — selected row indicator

Frequently asked

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

Why does my \x1b[7m text disappear inside the terminal's selection highlight?
The terminal's text-selection highlight is itself implemented as a reverse-video overlay. When you select a span that already has SGR 7 set, the two reverses XOR — the cell renders as positive image again and the reversed run becomes visually identical to the surrounding non-reversed text. This is intentional in xterm / iTerm2 / kitty / wezterm / ghostty and not a bug. If you need a selection-stable highlight, use an explicit fg / bg pair like \x1b[37;41m instead of SGR 7.
What cancels \x1b[7m\x1b[27m or \x1b[0m?
\x1b[27m is the surgical 'positive image' disable — it cancels reverse only, leaving fg / bg / bold / italic / underline / strikethrough intact. \x1b[0m cancels reverse too but also clears every other SGR attribute you set in the same span. Prefer \x1b[27m inside styled passages; reserve \x1b[0m for the end of a fully-styled chunk.

Terminal support

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

Related sequences