ANSI Escape Sequences for Terminal Customization

Overview

ANSI escape sequences are in-band signaling codes that control cursor position, text colors, and font styles in terminal emulators. They are the foundation underlying every customized terminal setup (Oh My Zsh, Starship, etc.).

Structure

Format: ESC[ + parameters + command letter (e.g., \033[31m = red text, \033[0m = reset)

Key Capabilities

CategoryExamples
Colors8/16 colors (30-37 FG, 40-47 BG), 256-color, 24-bit Truecolor
StylesBold (1), Faint (2), Italic (3), Underline (4), Blink (5), Inverse (7)
CursorMove to coordinates, save/restore position, hide/show
ScreenClear screen, clear line, scroll regions

Three Levels of Application

Level A: Custom Prompts (Quick Win)

Modify .bashrc/.zshrc PS1 with color codes — bold green username, blue directory, red indicator on command failure.

Level B: Script Enhancement

Create helper functions (print_error, print_success) that wrap text in ANSI codes. Turns “walls of text” into readable reports with visual hierarchy.

Level C: Understanding Modern Tools

  • grep --color=always and ls use ANSI codes internally
  • TUI libraries (Python rich, Go bubbletea, Rust ratatui) compile down to these sequences

Gotchas

  • Readability: Raw \033[31m is hard to debug — use variables (RED='\033[0;31m')
  • Portability: Some CI/CD logs or old terminals don’t interpret codes, showing garbage text