Introduction
zsh-syntax-highlighting colors your command line input in real-time as you type. Valid commands appear green, unknown commands red, strings are quoted in a distinct color, and paths that exist on disk get underlined. This immediate visual feedback catches typos before pressing Enter.
What zsh-syntax-highlighting Does
- Colors recognized commands, builtins, aliases, and functions in green
- Highlights unknown or misspelled commands in red as a visual error
- Applies distinct colors to strings, globbing patterns, and variables
- Underlines valid file paths and dims comments
- Updates highlighting character by character as you type
Architecture Overview
The plugin hooks into ZLE (Zsh Line Editor) via the zle-line-pre-redraw widget. On each buffer change, it tokenizes the current command line using Zsh's own lexer, classifies each token (command, argument, path, option), checks command existence via the hash table, then applies region_highlight entries to color each span.
Self-Hosting & Configuration
- Install via Oh My Zsh, Zinit, Zplug, Antigen, or Homebrew
- Must be sourced at the end of .zshrc (after completions and other plugins)
- Customize colors via ZSH_HIGHLIGHT_STYLES associative array
- Enable additional highlighters: brackets, pattern, cursor, line
- Pattern highlighter allows custom rules: highlight rm -rf in red
Key Features
- Main highlighter covers commands, arguments, paths, and redirections
- Brackets highlighter matches and colors paired parentheses and braces
- Pattern highlighter lets you define regex-based custom highlight rules
- Works with any color-capable terminal (256-color and truecolor)
- Minimal overhead: highlighting completes within the ZLE redraw cycle
Comparison with Similar Tools
- Fish shell — syntax highlighting built-in, but requires switching shells
- fast-syntax-highlighting — community fork with async and more token types
- zsh-autocomplete — focuses on completions rather than input coloring
- Syntax highlighting in Bash — no equivalent exists for Bash natively
FAQ
Q: Why must this plugin be sourced last in .zshrc? A: It wraps ZLE widgets. If other plugins modify widgets after it loads, highlighting may not trigger correctly.
Q: How do I change the color of valid commands? A: Add ZSH_HIGHLIGHT_STYLES[command]='fg=cyan,bold' to .zshrc before sourcing the plugin.
Q: Does it slow down typing in large prompts? A: No, tokenization is incremental and completes in microseconds for typical command lengths.
Q: Can I highlight dangerous commands like rm -rf differently? A: Yes, enable the pattern highlighter and add: ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')