Skip to content

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

6

Every 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.

SequenceParameterEffect
\e0x1BThe escape byte that starts every sequenceWritten \e in a shell, \033 in C, \x1b in most languages and \u001b in JSON and Java.
\e[CSIControl Sequence Introducer — opens a parameterised command
\e[<n>mmSelect Graphic Rendition: everything colour and style ends in m
\e[1;4;31m1;4;31Several parameters at once, separated by semicolonsBold, underlined and red in one sequence — shorter than three separate ones and identical in effect.
\e[0m0Clears 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 8A clickable hyperlink, in terminals that support oneAn OSC sequence rather than a CSI one, and terminated by ESC \ instead of a letter.

Text attributes

14

Support is uneven below the first five. Blink is ignored almost everywhere, and a few terminals render dim and italic identically.

SequenceParameterEffect
\e[1m1Bold or bright
\e[2m2Dim
\e[3m3Italic
\e[4m4Underline
\e[5m5BlinkWidely ignored; most modern terminals render it as plain text.
\e[7m7Reverse: swaps foreground and background
\e[8m8Hidden: the text is there but not drawn
\e[9m9Strikethrough
\e[21m21Double underlineSome terminals read 21 as “bold off” instead.
\e[22m22Normal intensity: cancels bold and dim
\e[23m23Cancels italic
\e[24m24Cancels underline
\e[27m27Cancels reverse
\e[29m29Cancels strikethrough

Text colour

17

The 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.

SequenceParameterEffect
\e[30m30Black text
\e[31m31Red text
\e[32m32Green text
\e[33m33Yellow text
\e[34m34Blue text
\e[35m35Magenta text
\e[36m36Cyan text
\e[37m37White text
\e[39m39Back to the default text colour
\e[90m90Bright black text
\e[91m91Bright red text
\e[92m92Bright green text
\e[93m93Bright yellow text
\e[94m94Bright blue text
\e[95m95Bright magenta text
\e[96m96Bright cyan text
\e[97m97Bright white text

Background colour

17

The same palette shifted by ten. A background code sets the cell behind the text and stays in effect until it is reset.

SequenceParameterEffect
\e[40m40Black background
\e[41m41Red background
\e[42m42Green background
\e[43m43Yellow background
\e[44m44Blue background
\e[45m45Magenta background
\e[46m46Cyan background
\e[47m47White background
\e[49m49Back to the default background
\e[100m100Bright black background
\e[101m101Bright red background
\e[102m102Bright green background
\e[103m103Bright yellow background
\e[104m104Bright blue background
\e[105m105Bright magenta background
\e[106m106Bright cyan background
\e[107m107Bright white background

256 colours and true colour

4

Two 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.

SequenceParameterEffect
\e[38;5;<n>m38;5;nText 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>m48;5;nBackground in palette colour n, 0–255
\e[38;2;<r>;<g>;<b>m38;2;r;g;bText in an exact 24-bit colour
\e[48;2;<r>;<g>;<b>m48;2;r;g;bBackground in an exact 24-bit colour

Cursor movement

11

Moves do not scroll and do not wrap: a cursor already at the top of the screen ignores a request to go up.

SequenceParameterEffect
\e[<n>AAUp n rows
\e[<n>BBDown n rows
\e[<n>CCRight n columns
\e[<n>DDLeft n columns
\e[<n>EEDown n rows, to the start of the line
\e[<n>FFUp n rows, to the start of the line
\e[<n>GGTo column n of the current row
\e[<row>;<col>HHTo an absolute row and column, counting from 1
\e[ssRemember the cursor position
\e[uuGo back to the remembered position
\e[6n6nAsk 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

7

Erasing writes spaces with the current background colour; it does not move the cursor.

SequenceParameterEffect
\e[0J0JClear from the cursor to the end of the screen
\e[1J1JClear from the start of the screen to the cursor
\e[2J2JClear the whole screenThe cursor stays where it was, which is why this is usually paired with \e[H.
\e[3J3JClear the scrollback buffer as well
\e[0K0KClear from the cursor to the end of the line
\e[1K1KClear from the start of the line to the cursor
\e[2K2KClear the whole line\r\e[2K is the usual way to redraw a progress line in place.

Terminal modes

8

Private 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.

SequenceParameterEffect
\e[?25l?25lHide the cursor
\e[?25h?25hShow the cursor
\e[?1049h?1049hSwitch to the alternate screenWhat a full-screen program uses so the shell's scrollback survives underneath it.
\e[?1049l?1049lSwitch back to the main screen
\e[?7h?7hWrap long lines at the right edge
\e[?7l?7lLet long lines run off the right edge
\e[?2004h?2004hTurn on bracketed pastePasted text arrives wrapped in \e[200~ and \e[201~, so a shell can tell typing from pasting.
\e[?2004l?2004lTurn off bracketed paste

FAQ

Why does my output stay coloured after the program ends?
Because nothing reset it. A colour code stays in effect until another one replaces it, so a program that exits mid-colour hands the shell a coloured prompt. Print \e[0m before exiting — including on the error path, which is the one people forget.
What is the difference between \e, \033 and \x1b?
Nothing. All three are the same byte, 0x1B, written the way a particular language spells it: \e in bash and zsh, \033 in C and in echo -e, \x1b in Python, Go, Rust and JavaScript, \u001b where only a Unicode escape is allowed, such as JSON and Java.
How do I strip these out of a log file?
Paste it into the ANSI stripper on this site. It removes colour, cursor and mode sequences and can also convert the colours to HTML or Markdown instead of dropping them, which is what you want if the point of the log was the highlighting.
Should I hard-code colours or use a library?
For anything more than a few lines, use a library — it checks whether the output is a terminal at all. Colour codes written to a pipe or a file end up in the file, and that is how build logs acquire their line noise.
Is bold the same as bright?
Historically yes, and on some terminals still. Code 1 was defined as bold, but many terminals implemented it by switching to the bright half of the palette instead, so bold red and bright red can render identically. The 90–97 range exists so you can ask for bright without asking for bold.