Bash – Automatically generate ZSH/Bash completion files

autocompletebashzsh

Does a tool exist that will automatically generate completion files for both bash and zsh?

I'd like to be able to specify one source of truth for my completions instead of maintaining a separate completion file for bash and zsh.

Best Answer

To complete short and long options and file names, make sure that the program generates a usage description with --help in the same format as GNU tools. Then set it up for completion with:

  • complete -F _longopt myprogram in bash
  • compdef _gnu_generic myprogram in zsh

If you want something more powerful, define bash completions and use zsh's bash compatibility mode. Users must call bashcompinit in their .zshrc, then they can call complete -F _myprogram_bash_complete myprogram in zsh as well as bash. Zsh only emulates a subset of bash, so test your completion function to ensure that it doesn't require features that zsh doesn't emulate.

Related Question