3

I'm using Arch Linux with zsh. I'm using devour as a wrapper for other commands, using it like devour cmd args.... I want zsh autocomplete to suggest cmd's flags/arguments after I've typed out cmd, making devour's autocomplete similar to sudo but without flags for devour. For example, typing devour id <TAB> should suggest flags for id.

I've checked /usr/share/zsh/functions/Completion/Unix/_sudo but I don't know much about zsh scripting for autocomplete so I don't know what I should extract from it to get the desired behavior.

How can I create an autocomplete script for such a wrapper?

1 Answer 1

1

If devour is just a prefix for other commands and doesn't take any options at all for itself, use the _precommand completion function. This is what zsh uses for eval, time, noglob, nohup, etc.

compdef _precommand devour

If the prefix command has options, the usual way to specify how to complete these options is the _arguments function. Pass at least -S -A "-*" to _arguments to indicate that no option to the prefix must be completed after the command name, and that -- can appear before the command name (which allows the command name to start with a dash). To specify that only external commands (and not aliases, functions or builtins) can be command names, specify that the completion for the first argument (1) is _command_names -e. Supply a completion for the other non-option arguments (*) that dispatches to _normal -p $service. Note that _arguments does some very complex parsing, and punctuation and whitespace are significant in places where you might not expect it to be. (The effect of getting it wrong can range from not getting completions at all, to very minor issues like menu completion not displaying expected hints or completing the wrong thing in certain specific nested contexts.) If in doubt, copy from the snippet below or from a completion function that ships with zsh.

#compdef my_prefix
_arguments -S -A '-*' : \
  '1:command: _command_names -e' '*:: : _normal -p $service'

_fakeroot and _setsid are some nice, simple examples. Some more elaborate examples include _sudo and _xargs.

See Dynamic zsh autocomplete for custom commands for generic advice on how and where to put the completion function.

Note that this answer assumes that you're using the “new-style” (added mid-1990s) completion system, with compinit in your .zshrc.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .