Skills2026年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 dry-run、列出写入项,确认后再继续。

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

先 dry-run,确认写入项后再运行此命令。

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

讨论

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

相关资产