ANSI escape codes
An ANSI escape code is a short run of characters a program prints to tell the terminal to do something other than show those characters — turn the next words red, move the cursor up two lines, clear the screen. The terminal swallows the sequence and acts on it, which is why a log file full of them looks like line noise when you open it in an editor.
Every code below starts with the escape byte and, with one exception, an opening bracket. The parameter column gives the part between the bracket and the final letter, which is the piece you assemble in code; the first column shows the whole sequence as you would write it in a shell.
Support varies by terminal, and this page says so where it matters. The eight basic colours and bold work everywhere; blink, double underline and true colour do not.
Anatomy of a sequence
6Every code on this page is the same shape: the escape byte, an opening bracket, some parameters, and one letter that says what to do with them.
| Sequence | Parameter | Effect |
|---|---|---|
| \e | 0x1B | The escape byte that starts every sequenceWritten \e in a shell, \033 in C, \x1b in most languages and \u001b in JSON and Java. |
| \e[ | CSI | Control Sequence Introducer — opens a parameterised command |
| \e[<n>m | m | Select Graphic Rendition: everything colour and style ends in m |
| \e[1;4;31m | 1;4;31 | Several parameters at once, separated by semicolonsBold, underlined and red in one sequence — shorter than three separate ones and identical in effect. |
| \e[0m | 0 | Clears every attribute back to the terminal defaultEmit this at the end of coloured output. A program that exits without it leaves the user's prompt coloured. |
| \e]8;;URL\e\\text\e]8;;\e\\ | OSC 8 | A clickable hyperlink, in terminals that support oneAn OSC sequence rather than a CSI one, and terminated by ESC \ instead of a letter. |
Text attributes
14Support is uneven below the first five. Blink is ignored almost everywhere, and a few terminals render dim and italic identically.
| Sequence | Parameter | Effect |
|---|---|---|
| \e[1m | 1 | Bold or bright |
| \e[2m | 2 | Dim |
| \e[3m | 3 | Italic |
| \e[4m | 4 | Underline |
| \e[5m | 5 | BlinkWidely ignored; most modern terminals render it as plain text. |
| \e[7m | 7 | Reverse: swaps foreground and background |
| \e[8m | 8 | Hidden: the text is there but not drawn |
| \e[9m | 9 | Strikethrough |
| \e[21m | 21 | Double underlineSome terminals read 21 as “bold off” instead. |
| \e[22m | 22 | Normal intensity: cancels bold and dim |
| \e[23m | 23 | Cancels italic |
| \e[24m | 24 | Cancels underline |
| \e[27m | 27 | Cancels reverse |
| \e[29m | 29 | Cancels strikethrough |
Text colour
17The eight standard colours and their bright counterparts. What the reader actually sees is their terminal theme's take on each — “red” is a slot in a palette, not a hex value.
| Sequence | Parameter | Effect |
|---|---|---|
| \e[30m | 30 | Black text |
| \e[31m | 31 | Red text |
| \e[32m | 32 | Green text |
| \e[33m | 33 | Yellow text |
| \e[34m | 34 | Blue text |
| \e[35m | 35 | Magenta text |
| \e[36m | 36 | Cyan text |
| \e[37m | 37 | White text |
| \e[39m | 39 | Back to the default text colour |
| \e[90m | 90 | Bright black text |
| \e[91m | 91 | Bright red text |
| \e[92m | 92 | Bright green text |
| \e[93m | 93 | Bright yellow text |
| \e[94m | 94 | Bright blue text |
| \e[95m | 95 | Bright magenta text |
| \e[96m | 96 | Bright cyan text |
| \e[97m | 97 | Bright white text |
Background colour
17The same palette shifted by ten. A background code sets the cell behind the text and stays in effect until it is reset.
| Sequence | Parameter | Effect |
|---|---|---|
| \e[40m | 40 | Black background |
| \e[41m | 41 | Red background |
| \e[42m | 42 | Green background |
| \e[43m | 43 | Yellow background |
| \e[44m | 44 | Blue background |
| \e[45m | 45 | Magenta background |
| \e[46m | 46 | Cyan background |
| \e[47m | 47 | White background |
| \e[49m | 49 | Back to the default background |
| \e[100m | 100 | Bright black background |
| \e[101m | 101 | Bright red background |
| \e[102m | 102 | Bright green background |
| \e[103m | 103 | Bright yellow background |
| \e[104m | 104 | Bright blue background |
| \e[105m | 105 | Bright magenta background |
| \e[106m | 106 | Bright cyan background |
| \e[107m | 107 | Bright white background |
256 colours and true colour
4Two extensions to the same SGR mechanism. Both are widely but not universally supported; a terminal that does not understand them usually ignores the sequence rather than printing it.
| Sequence | Parameter | Effect |
|---|---|---|
| \e[38;5;<n>m | 38;5;n | Text in palette colour n, 0–2550–15 are the sixteen above, 16–231 a 6×6×6 colour cube, 232–255 a greyscale ramp. |
| \e[48;5;<n>m | 48;5;n | Background in palette colour n, 0–255 |
| \e[38;2;<r>;<g>;<b>m | 38;2;r;g;b | Text in an exact 24-bit colour |
| \e[48;2;<r>;<g>;<b>m | 48;2;r;g;b | Background in an exact 24-bit colour |
Cursor movement
11Moves do not scroll and do not wrap: a cursor already at the top of the screen ignores a request to go up.
| Sequence | Parameter | Effect |
|---|---|---|
| \e[<n>A | A | Up n rows |
| \e[<n>B | B | Down n rows |
| \e[<n>C | C | Right n columns |
| \e[<n>D | D | Left n columns |
| \e[<n>E | E | Down n rows, to the start of the line |
| \e[<n>F | F | Up n rows, to the start of the line |
| \e[<n>G | G | To column n of the current row |
| \e[<row>;<col>H | H | To an absolute row and column, counting from 1 |
| \e[s | s | Remember the cursor position |
| \e[u | u | Go back to the remembered position |
| \e[6n | 6n | Ask the terminal where the cursor isThe terminal answers on standard input as \e[<row>;<col>R — the one sequence here that produces a reply. |
Erasing
7Erasing writes spaces with the current background colour; it does not move the cursor.
| Sequence | Parameter | Effect |
|---|---|---|
| \e[0J | 0J | Clear from the cursor to the end of the screen |
| \e[1J | 1J | Clear from the start of the screen to the cursor |
| \e[2J | 2J | Clear the whole screenThe cursor stays where it was, which is why this is usually paired with \e[H. |
| \e[3J | 3J | Clear the scrollback buffer as well |
| \e[0K | 0K | Clear from the cursor to the end of the line |
| \e[1K | 1K | Clear from the start of the line to the cursor |
| \e[2K | 2K | Clear the whole line\r\e[2K is the usual way to redraw a progress line in place. |
Terminal modes
8Private modes, marked by the question mark. They change how the terminal behaves rather than how one piece of text looks, and every one of them has to be turned back off before the program exits.
| Sequence | Parameter | Effect |
|---|---|---|
| \e[?25l | ?25l | Hide the cursor |
| \e[?25h | ?25h | Show the cursor |
| \e[?1049h | ?1049h | Switch to the alternate screenWhat a full-screen program uses so the shell's scrollback survives underneath it. |
| \e[?1049l | ?1049l | Switch back to the main screen |
| \e[?7h | ?7h | Wrap long lines at the right edge |
| \e[?7l | ?7l | Let long lines run off the right edge |
| \e[?2004h | ?2004h | Turn on bracketed pastePasted text arrives wrapped in \e[200~ and \e[201~, so a shell can tell typing from pasting. |
| \e[?2004l | ?2004l | Turn off bracketed paste |