home blog portfolio Ian Fisher

ANSI codes

Special control sequences of bytes that a program can write to a terminal device, for example to change the color of text or hide the cursor. A form of in-band signaling. Complex text user interfaces like Vim can be built with ANSI codes, usually via an abstraction like the curses library.

Cheatsheet

Colors

General form: \033[Xm (or \x1b[Xm) where X is the code

black:    30
red:      31
green:    32
yellow:   33
blue:     34
magenta:  35
cyan:     36
white:    37
reset:     0

Cursor control

General form: \033[ followed by command. n and m stands for integers, not for literal characters.

nA      move `n` rows up
nB      move `n` rows down
nC      move `n` columns right
nD      move `n` columns left
n;mH    set cursor position to row `n`, column `n` (1-indexed)
2J      clear the screen
0K      clear to end of line
1K      clear to beginning of line
2K      clear line
?25h    show the cursor
?25l    hide the cursor

Bibliography