compsys: Colorizing completion lists

Zsh is able to complete in color. This is done with zstyle, too.

The complist module is needed to get colored completion. It can be loaded with `zmodload zsh/complist'. The following is one advanced example. You need to do "setopt extendedglob" for it to work:

zstyle ':completion:*:*:kill:*:processes' list-colors "=(#b) #([0-9]#)*=36=31"

In the string after the word "list-colors" you see 3 equal-letters. The first equal marks the beginning of the zsh regular expression which describes what exactly should be colored. '(#b)' enables the use of parentheses through backreferences. These brackets are needed for different colorings of words. So every brackets-pair means: Color the words inside in a different color!

' #' in the zsh regular expression means 0 to arbitrary number of spaces where the '#' would have been a '*' in a usual regular expression like perl. The second equal always marks the ansicolor-escape-number for the default color which matches every time when there is no special expression for a completion color. Every following `=' signals the color escape-number for one brackets-pair. The example above only has one special ansi-color(31=red) besides the default color 36=cyan.

Now this example shows that zstyle one-liners can be quite stuffed. To get a better overview I suggest using variables and perhaps even using more than one line through use of \ at the end of one line (continuation lines). Don't confuse the 2 different meanings of '#' here each of which does not mean "comment"!

Instead of using color-numbers like 36 and 31 you can use the names of colors, too. This is done thru an associative array-variable named "colors" which is available after doing 'autoload colors'. Now you can use explicit color-names:

zstyle ':completion:*:*:kill:*:processes' list-colors "=(#b) #([0-9]#)*=$color[cyan]=$color[red]"

To sum it all up I just use variables plus continuation lines next:

zstyle ":completion:*:*:$command:*:$tag" list-colors "=(#b)\
=$color_number_for_letters_in_second_bracket-pair"     "..."

You can use more than the two brackets of course. I use "…" because list-colors can have more than one argument. This is needed if e.g. you like to have different letters in more than one line uniquely colored. See `man zshmodules | less -p "THE ZSH/COMPLIST MODULE"' for all details.

examples/compsys/colors.txt · Last modified: 2010/01/05 09:20 (external edit)