SGR Select Graphic Rendition
Set the colors and attributes of the characters written after it.
CSI Pm m
- Defaults
- Pm = 0
- ECMA-48
- §8.3.117 SGR – Select Graphic Rendition, p. 61
- DEC STD 070
- §5.6.2 Select Graphic Rendition, p. 5-103
- VT510
- SGR—Select Graphic Rendition
Set how the characters written from now on look. Pm is a list of parameters, applied in order, so one SGR can set several things at once. CSI m with no parameters is CSI 0 m, which resets everything.
| Parameter | Sets | Undone by |
|---|---|---|
1 | bold | 22 |
2 | faint | 22 |
3 | italic | 23 |
4 | underline | 24 |
5 | blink | 25 |
7 | inverse | 27 |
8 | invisible | 28 |
9 | strikethrough | 29 |
30–37, 90–97 | foreground, from the 16 basic colors | 39 |
40–47, 100–107 | background, likewise | 49 |
38;5;N, 48;5;N | color N of the 256-color palette | 39, 49 |
38;2;R;G;B, 48;2;R;G;B | a direct RGB color | 39, 49 |
The 256-color and direct forms come from ITU T.416, which separates their parts with colons: 38:2::R:G:B, with an empty field for a color space nobody uses. Terminals accept both. Colons also carry underline styles, as 4:2 double, 4:3 curly, 4:4 dotted and 4:5 dashed.
Attributes belong to the characters written, not to the cells: moving the cursor does not change them, and writing over a cell replaces its attributes with the current ones.
Validation
SGR-1: Bold and a color
\e[1;31m
A
\e[0m
B
|AB___|
attr 1,1 bold fg=1
attr 1,2 plain
SGR-2: Undoing one attribute
\e[1;3;4m # bold, italic, underline
A
\e[23m # not italic
B
|AB___|
attr 1,1 bold italic underline
attr 1,2 bold -italic underline
SGR-3: 256 colors and direct color
\e[38;5;208m
A
\e[38;2;90;180;250m
B
\e[48:2::200:100:0m
C
|ABC__|
attr 1,1 fg=208
attr 1,2 fg=#5ab4fa
attr 1,3 fg=#5ab4fa bg=#c86400
SGR-4: Underline styles
\e[4:3m
A
\e[4:2m
B
\e[24m
C
|ABC__|
attr 1,1 underline=curly
attr 1,2 underline=double
attr 1,3 -underline
SGR-5: Inverse
\e[32;7m
A
|A____|
attr 1,1 inverse fg=2