I have written a simple completion script for a command cheat so that when using cheat at a prompt zsh will autocomplete arguments.
(obviously) this needed to be setup in a compinit / compsys compatible way:

file: ~/system/dev_resources/zsh_comp/_cheat

#compdef cheat

[bulk of code...]

# make call into compsys to provide completion!
_describe 'values' comp_cands_arr

avail the script to zsh through ~/.zshrc:

fpath=(~/system/dev_resources/zsh_comp $fpath)
autoload -U ~/system/dev_resources/zsh_comp/*(:t)

The completion script works fine - no problems.
Now I simply want to reference and configure this completion script through the zstyle builtin.
The COMPLETION SYSTEM CONFIGURATION section in zsh compsys documentation tells us:

When completion is attempted somewhere on the command line the completion system begins building the context [...]
The fields are always in the order


[completer] The completer currently active, the name of the function without the leading underscore and with other underscores converted to hyphens. A ‘completer’ is in overall control of how completion is to be performed;

My question is, is my zsh completion script _cheat considered a "completer" in the context string? i.e. would this be correct/applicable?


It seems to be applicable, but at the same time the _cheat script is not a formally defined as a shell function, its just a loose collection of commands...


Your _cheat file does define a function called _cheat. You can verify that by displaying the function's code (echo $functions[_cheat]). When a function is autoloaded from a file, the file contains the function's code, as if there was _cheat () { before the file content and } after the file content. (Zsh also supports “ksh-style” autoloading where the file must contain a function definition plus a call to the function itself.)

The magic first line #compdef found in a file in $fpath when the completion system is initialized is equivalent to calling compdef _cheat cheat where _cheat is the name of the file (and autoloaded function) and cheat is the name of the command that this function is to be used for. The association is recorded in the _comps associative array ($_comps[cheat]=_cheat).

The completer field in the zstyle context string is the name of the completion function with a leading underscore removed.

| improve this answer | |
  • thanks Gilles! when I run echo $functions[_cheat] zsh gives builtin autoload -XU. If I run % declare -f _cheat gives _cheat () { # undefined .... builtin autoload -XUz } reading your autoload link - it seems autoloaded functions are not defined until they are invoked (hence autoloading) - So then I invoke the function by getting zsh to do a completion . then run echo $functions[_cheat] and BAM! function definition! – the_velour_fog Mar 18 '16 at 0:30

In answer to the question

is my zsh completion script _cheat considered a "completer" in the context string?

The answer seems to be no
If you use ESC2, CTRL+x,h to dump zsh's completion context

% cheat b                                                                                                                             
tags in context :completion::complete:cheat::
    values  (_describe _cheat)

You can see that zsh considers the third/completer position to belong to the completer "complete" and that cheat is assigned the fourth or command position.
Importantly this is the command cheat (the actual executable program) and not the completion function _cheat, the completion function _cheat
is a value as defined by zsh's zstyle builtin.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.