2014-10-10: ZSH “show-ambiguity” style

Highlighting the first ambiguous character in completions

Emacs 23.1 introduced several changes to completion. Among those, highlights are added to completion lists in order to visualize the first ambiguous/non-matching character. To better visualize this behavior, see the following animation:

emacs_hl_amb.gif

Emacs 23 performing ambiguous character highlighting during file-name completion.

You can see in the animation how it behaves in a directory tree containing many similarly-named files: typing ~/test/201408/ expands to ~/test/201408/201408 and the highlight (by default an underscore) readily shows the next difference after that.

The usefulness of this small tweak cannot be overstated. Since the default completion style expands the prompt up to the first ambiguity, the highlight is in fact exactly on the character that needs to be typed in order to make the completion continue.

Previously, you would need to look at the minibuffer to see what the prompt expanded to, and then scan visually the buffer to play “spot the difference”. Granted, there are smarter ways to make the completion unique, but the highlight makes any completion list much more convenient to scroll through.

It’s unfortunately not well explained, but since ZSH 5.0.6 you can have the same behavior for any ZSH completion list, as shown below:

zsh_hl_amb.gif

ZSH 5.0.6 performing ambiguous character highlighting during file-name completion.

It can be enabled by setting the “show-ambiguity” style appropriately:

zmodload zsh/complist
autoload -U compinit
compinit -u
zstyle ':completion:*' show-ambiguity true

show-ambiguity” accepts an LS_COLORS specification. You can colorize the highlight using an ansi color code, or by using the “colors” function which provides convenient aliases for all color values:

autoload -U colors
colors
zstyle ':completion:*' show-ambiguity "$color[fg-red]"

You can combine multiple ansi attributes with “;”:

# For reference:
# 1: bold, 2: underline, 5: standout, 7: reverse

# bold/underlined
zstyle ':completion:*' show-ambiguity "1;2"

# bold/red
zstyle ':completion:*' show-ambiguity "1;$color[fg-red]"

There is no error checking for the value of “show-ambiguity”, so a wrong setting will likely result in a broken prompt.

Regular colorization and ambiguous highlighting cannot currently coexist: ambiguous highlighting takes over regular file colorization when there is an ambiguity.

This happens because the style is based on top of the “complist” module which is regularly used to colorize file lists. In essence, the text up to the current cursor position is used to build a matching glob pattern. The resulting entry is then temporarily prepended to the default list-colors for “complist” to highlight. For additional details, see the development thread:

http://www.zsh.org/mla/users/2014/msg00177.html