shellzshzshrczsh-completion

ZSH Equivalent to Bash `_command` completion


Given an example wrapper shell function:

app() {
    setsid "$@"
}

In Bash, you can do complete -F _command app to automatically give app completions for all commands (not just binaries on the path but also the arguments afterwards).

How can you do this in ZSH? I have some leads with _normal and -command-line- but I've been unable to figure out the correct command to put in a .zshrc.

Update:

Just wanted to document something interesting, this produces a SIGSEGV:

autoload -U compinit && compinit
compdef _normal app

# Attempt tab completion of app:
# _normal:1: maximum nested function level reached; increase FUNCNEST?

FUNCNEST=999

# Attempt tab completion of app again
# !!! ZSH crashes (see coredumpctl) !!!

Solution

  • Updated shorter answer:

    Turns out _command actually does exist in ZSH as well! I didn't find the manpages very clear on this but here's your answer:

    # Set up autocomplete
    autoload -U compinit && compinit
    compdef _command app
    

    It's also helpful to note that after compinit you can run echo $_comps[ls] to get the completion command of any command (the output here is _ls).

    Original answer:

    Figured it out:

    # Set up autocomplete
    _app() {
        shift words
        (( CURRENT-- ))
        _normal
    }
    autoload -U compinit && compinit
    compdef _app app
    

    This was somewhat a duplicate of this question: How do I dynamically select a completion function in zsh?

    If someone knows a shorter solution like what Bash has then please answer as that would be nice.