Lesson 5 of 7

Colors, Cursors & Full-Screen Apps

Learn how escape codes let programs color text, move the cursor and draw full-screen apps like vim, even though the only receives .

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 such as vim, htop and top constantly send cursor-movement and redraw codes while reading your keys directly.

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.
Finished reading?Mark it done to track your progress.

Tip: use the ← and → keys to move between lessons.