Completion is function-based: on Tab zsh finds a completion function matching the command name, and it must exist. Zsh reads no --help.
Minimum setup:
| |
compinit loads completion and builds a dump file, ~/.zcompdump. Rebuild it if changes do not appear, or zsh warns about an insecure completion directory:
| |
Git completion
Git support is zsh’s own _git function: once compinit has run, these complete on Tab with refs, remotes and branches:
| |
Oh My Zsh enables it; not required.
Who ships the script
A tool’s completion file is named after the command — _myapp — on $fpath, usually ~/.zsh/completions/ or the package manager’s site-functions directory. Modern CLIs generate it:
| |
Cobra (Go), clap (Rust), yargs or oclif (Node) emit one; writing your own is the fallback for internal tools.
Completion for an alias
Shell alias for git — alias g=git — must reuse git’s completion:
| |
Keep completealiases off, or zsh expands the alias before completion runs and the binding never matches:
| |
Git’s own aliases — co = checkout — are handled by the official script via git help --aliases-for-completion.
Git aliases that shell out — starting with ! — are opaque shell commands; attach completion to a shell function:
| |
Branch completion in your own command
| |
Add refs/remotes to for-each-ref for remote branches; scope to one subcommand via $words[2]. A branch argument followed by more:
| |
Helpers by type: _git-branch/_git-switch refs, _files paths, _git-remote remotes.
compdef does nothing until compinit has run.