>_How the Terminal Works · Lesson 5 of 7

Colors, Cursors & Full-Screen Apps

Terminals only move bytes — so how does text turn green? How does vim own the whole screen? Answer: some bytes are secret commands. Escape codes are the terminal's hidden control language.

Bash
# Byte 27 (ESC) starts a command; '[' + codes + 'm' = style:
printf '\033[32mgreen text\033[0m normal\n'
printf '\033[1;31mbold red\033[0m\n'
printf '\033[7minverted\033[0m\n'

# Codes: 0 reset | 1 bold | 31 red | 32 green | 34 blue
#        90-97 bright colors | 38;5;N -> 256 colors

# It's just bytes in the stream — look:
ls --color=always | head -3 | cat -v
# ^[[0m^[[01;34msrc^[[0m ...   <- the 'invisible' codes

Beyond colors, escape codes move the cursor to any row and column, clear regions, and switch screens. Progress bars are 'carriage return, redraw the line'. Spinners are 'print, back up, print'. Full-screen apps — vim, htop, top — are programs furiously emitting cursor-movement and redraw codes while reading your keys raw. The terminal is a canvas addressed by text.

Bash
# A live progress bar in four lines of shell:
for i in $(seq 1 20); do
  printf '\r[%-20s] %d%%' "$(printf '#%.0s' $(seq 1 $i))" $((i*5))
  sleep 0.1
done; echo
# \r returns the cursor to line start — each print overwrites

# Terminal modes: normally the OS buffers a full line and
# handles Backspace before programs see anything ('cooked').
# vim switches to raw mode — every keystroke delivered
# instantly, no Enter needed. 'stty sane' + Enter rescues a
# terminal a crashed program left in a weird state.
◆ Note
Ctrl+C isn't 'copy' here for a reason older than clipboards: the TTY layer turns it into a SIGINT signal that interrupts the running program. Ctrl+Z suspends (SIGTSTP; resume with fg), Ctrl+D sends 'end of input'. That's also why terminal copy/paste grew different bindings (Ctrl+Shift+C or Cmd+C).