1 Star 1 Fork 0

mirrors / zsh-syntax-highlighting

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
regexp.md 2.52 KB
AI 代码解读
一键复制 编辑 原始数据 按行查看 历史
Matthew Martin 提交于 2023-10-25 21:33 . *: Use https in URLs

zsh-syntax-highlighting / highlighters / regexp

This is the regexp highlighter, that highlights user-defined regular expressions. It's similar to the pattern highlighter, but allows more complex patterns.

How to tweak it

To use this highlighter, associate regular expressions with styles in the ZSH_HIGHLIGHT_REGEXP associative array, for example in ~/.zshrc:

typeset -A ZSH_HIGHLIGHT_REGEXP
ZSH_HIGHLIGHT_REGEXP+=('^rm .*' fg=red,bold)

This will highlight lines that start with a call to the rm command.

The regular expressions flavour used is PCRE when the RE_MATCH_PCRE option is set and POSIX Extended Regular Expressions (ERE), as implemented by the platform's C library, otherwise. For details on the latter, see the zsh/regex module's documentation and the regcomp(3) and re_format(7) manual pages on your system.

For instance, to highlight sudo only as a complete word, i.e., sudo cmd, but not sudoedit, one might use:

  • When the RE_MATCH_PCRE is set:

    typeset -A ZSH_HIGHLIGHT_REGEXP
    ZSH_HIGHLIGHT_REGEXP+=('\bsudo\b' fg=123,bold)
  • When the RE_MATCH_PCRE is unset, on platforms with GNU libc (e.g., many GNU/Linux distributions):

    typeset -A ZSH_HIGHLIGHT_REGEXP
    ZSH_HIGHLIGHT_REGEXP+=('\<sudo\>' fg=123,bold)
  • When the RE_MATCH_PCRE is unset, on BSD-based platforms (e.g., macOS):

    typeset -A ZSH_HIGHLIGHT_REGEXP
    ZSH_HIGHLIGHT_REGEXP+=('[[:<:]]sudo[[:>:]]' fg=123,bold)

Note, however, that PCRE and POSIX ERE have a large common subset: for instance, the regular expressions [abc], a*, and (a|b) have the same meaning in both flavours.

The syntax for values is the same as the syntax of "types of highlighting" of the zsh builtin $zle_highlight array, which is documented in the zshzle(1) manual page.

See also: regular expressions tutorial, zsh regexp operator =~ in the zshmisc(1) manual page

1
https://gitee.com/minhanghuang/zsh-syntax-highlighting.git
git@gitee.com:minhanghuang/zsh-syntax-highlighting.git
minhanghuang
zsh-syntax-highlighting
zsh-syntax-highlighting
master

搜索帮助