Command completion in Zsh depends on the shell's completion system being initialized before the terminal can suggest subcommands, files, options, and command-specific arguments. Without that setup, pressing Tab may fall back to basic filename completion or show no command-aware choices.
The completion system is loaded with autoload -Uz compinit and initialized with compinit. During initialization, Zsh scans the completion function path and writes a dump file so later interactive shells can start faster. The -d option names that dump file explicitly.
Keep this setup in ~/.zshrc because completion is an interactive shell feature. A new interactive Zsh process should then register built-in command completions such as cd_completion=_cd and create the configured .zcompdump file. If Zsh reports insecure completion directories, inspect them with compaudit and correct ownership or permissions before bypassing the warning.
Related: How to configure Zsh startup files
Related: How to configure Zsh completion styles
Related: How to generate shell completions for Codex
Steps to enable command completion in Zsh:
- Confirm that Zsh is available.
$ zsh --version zsh 5.9 (aarch64-unknown-linux-gnu)
- Back up ~/.zshrc before editing completion setup.
$ cp ~/.zshrc ~/.zshrc.bak
If ~/.zshrc does not exist yet, create it with touch ~/.zshrc and skip the backup.
- Add the completion initialization lines to ~/.zshrc.
- ~/.zshrc
autoload -Uz compinit compinit -d "${ZDOTDIR:-$HOME}/.zcompdump"
The -d path keeps the completion dump under the active Zsh config directory when ZDOTDIR is set, and under the home directory otherwise.
- Check the edited file for syntax errors.
$ zsh -n ~/.zshrc
No output from zsh -n means Zsh parsed the startup file without finding a syntax error.
- Start a new interactive Zsh process and confirm that completion data loaded.
$ zsh -ic 'print cd_completion=${_comps[cd]:-missing}' cd_completion=_cdThe cd_completion=_cd output shows that Zsh registered the built-in completion function for cd.
- Confirm that the completion dump file exists after initialization.
$ ls -l "${ZDOTDIR:-$HOME}/.zcompdump" -rw-r--r-- 1 operator operator 50559 Jun 5 08:19 /home/operator/.zcompdump - Run compaudit if compinit reports insecure directories.
$ zsh -ic 'compaudit'
No output means compaudit did not list insecure completion paths.
Do not silence insecure-directory warnings until the listed directories are owned by the expected user or by root and are not writable by other users.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.