Configs2026年5月17日·1 分钟阅读

zsh-syntax-highlighting — Fish-Style Syntax Highlighting for Zsh

A Zsh plugin that provides real-time syntax highlighting of commands as you type, coloring valid commands green and errors red before execution.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Needs Confirmation · 66/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
zsh-syntax-highlighting Overview
通用 CLI 安装命令
npx tokrepo install 8019b3c2-51eb-11f1-9bc6-00163e2b0d79

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')

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产