zsh-syntax-highlighting/docs/highlighters/regexp.md
Nuri Jung 471b46312f
Add general example and remove details
This adds a general, platform independent example before the more detailed
example.

Also removes details about the reason of the platform dependency of some
non-standard regular expressions, and replaces it with a hyperlink to the related
Zsh document.
2020-07-03 15:10:57 +09:00

1.8 KiB

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 the whole line starting with rm (for all operating systems, in contrast to the below example).

Some regex patterns are subject to the host platform, especially the kernel. To highlight sudo only as a complete word, i.e., sudo cmd, but not sudoedit:

  • GNU-Linux

    typeset -A ZSH_HIGHLIGHT_REGEXP
    ZSH_HIGHLIGHT_REGEXP+=('\<sudo\>' fg=123,bold)
    
  • BSD-based platforms

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

Both would give the same results, but do not work on each other's system.

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