Tab completion of options for every command that has no completion
definition of its own. Pressing Tab on a word starting with - reads the
command's help output and suggests its options; if there are no
suggestions, zsh falls back to its normal behavior (file/directory completion).
Warning
To read a command's options, this plugin runs the command (tool --help,
tool sub --help). --help is a convention, not a guarantee: a program that
doesn't handle it may do its normal job instead. By default the plugin
asks you before running any program for the first time, and again if the
program changed since you allowed it. Read Safety before turning
that off.
$ claude --sys<Tab>
--system-prompt --system-prompt-snapshot
$ ./script.py --verb<Tab>
$ ./script.py --verbose
Works for commands in PATH, for executables given by path (./script.py)
and through aliases, and for subcommands: tool sub --<Tab> reads
tool sub --help.
$ claude mcp add --tra<Tab>
$ claude mcp add --transport
Subcommand names and option values are completed too:
$ mytool <Tab>
build -- Compile the project
deploy -- Deploy it
$ mytool deploy --env <Tab>
production staging
-
Scripts that don't parse their arguments.
./cleanup.sh --<Tab>runscleanup.sh --help. A script that never looks at its arguments ignores--helpand** does its job** . Many small shell scripts are like that. -
Scripts that dispatch subcommands by hand. A
manage.shwithcase "$1" in deploy) ...that listsdeployin its usage text would run**manage.sh deploy** for./manage.sh deploy --<Tab>, if it ignores--help. -
Short help flags.
-hisn't "help" everywhere (host, human-readable, halt), and-helpmay be read as-h -e -l -p. -
Untrusted directories. In a fresh clone or a downloads folder,
./x --<Tab>runs code you haven't read. -
Side effects of
--helpitself. Some tools create config files, check for updates or make network requests before printing help.
It asks first (default). The first time a program would be run, the plugin asks below the command line:
zsh-help-complete: run "mytool --help" (/usr/local/bin/mytool) to read its options? [y]es [o]nce [n]ot now ne[v]er
y allows it from now on,o for this shell only,v never.n or any
other key (including a second Tab pressed out of habit) runs nothing and
doesn't ask again in this shell. The full path is shown so you can see
which file would run. An answer covers the program and its subcommands. #
It asks again when the program changed. An answer is kept together with
the file's mtime, size and inode. If the file changed since you allowed it
(an update, or someone replaced it), you are asked again and the question
saysCHANGED . #
It checks scripts before running them. The first 256 KB of the file is read:
- Shell scripts are run only if they show signs of handling
--help(--help,usage,getopts) or just pass their arguments on (exec ... "$@"). Their subcommands are never run:manage.sh deploy --usesmanage.sh --help. - Scripts given by path (
./x) in other languages need an argument parser (argparse,click,typer,docopt,commander,yargs, ...) or signs of handling--help. - Compiled programs and installed (
PATH) scripts in other languages are run as before. - Shell scripts are run only if they show signs of handling
Only --help by default.-h ,-hh ,-help are tried only for
commands you configure (seeSettings ). #
A denylist.rm ,dd ,mkfs ,shred ,shutdown ,reboot ,kill ,fdisk and similar are never run. Entries may name a subcommand:'mytool deploy' . #
stdin is closed and there's a timeout (5 s), so a program can't wait for input or freeze the shell. A timeout doesn't undo anything a program already did, though. #
An optional sandbox. Withzstyle ':zhc:*' sandbox yes the help call
runs read-only and without network: underbubblewrap (bwrap ) on Linux,sandbox-exec on macOS. If neither is available, nothing is run. #
Help is run once per program version. The output is cached; nothing is run on later Tabs until the program changes.
zhc allow mytool ./build.sh # allow without being asked
zhc deny sometool # never run it
zhc forget mytool # ask again next time
zhc list # show the answers
zhc clear-cache # remove the cached help output
Answers are kept in ~/.config/zsh-help-complete/decisions
($XDG_CONFIG_HOME is respected).
Only words starting with - ask. Subcommand names and option values
(tool <Tab>, tool --env <Tab>) use the help only of programs you
allowed before; for any other program, Tab just completes files as usual,
without asking or running anything.
If you trust everything you complete, zstyle ':zhc:*' ask no turns the
question off. Then the script check, denylist, timeout and --help-only
default are the only protection, so consider also
zstyle ':zhc:*' allow-paths no, which runs only commands found in PATH.
zsh's built-in _gnu_generic runs --help too, but:
- When
--helpoutput contains bracketed lines such as--system-prompt[-file],, it fails completely with_arguments:comparguments:327: invalid option definition, or such a line silently swallows the real option of the same name (--system-prompt). This plugin removes those lines from the help text before it is parsed, while keeping GNU style optional values like--color[=WHEN]. - A command that doesn't understand
--helpand hangs will freeze the shell. Here--helpruns with a timeout and with stdin closed (needs GNUtimeout; on macOS runbrew install coreutilsto getgtimeout). - It never asks, and it has no denylist or script check.
A word right after the command (up to three levels: tool run fast --) is
treated as a subcommand when the command's help output lists it at the start
of an indented line, as in
Commands:
build Compile the project
run, r Run it
Then tool build --<Tab> completes the options of tool build --help. Other
words (mv file.txt --) are ordinary arguments and the command's own help is
used. Each subcommand has its own cache. Words after an option
(tool --verbose build --) are not checked for subcommands. Shell scripts'
subcommands are never run (see Safety).
tool <Tab> lists the subcommands from the help output, with their
descriptions, followed by files. Sections like Commands:,
Available Commands: (Go/cobra), Commands: with update|upgrade aliases
(Node/commander) and argparse's {serve,build} are read; wrapped description
lines are not mistaken for names. This needs the program to be allowed
already (see Safety).
Options that take a value are recognized in the forms --tag TAG,
--tag=TAG, --env {a,b}, --color <WHEN> and --name string (Go), so
tool --tag v1 --<Tab> skips the value. Then:
- listed choices are offered: argparse
{staging,production}, clap[possible values: auto, always, never], commander(choices: "fast", "careful"), also when they continue on the next line; - values named like
DIRcomplete directories,FILEorPATHfiles; - other values (
TAG) complete nothing, so you don't get a misleading file list.
Commands with their own _foo completion (git, docker, ...) keep it. Only
when that completion finds nothing for a word starting with - does the
plugin step in and read the options from the help output (asking first, as
always). For example, zsh 5.9's _mv declares --backup= as taking a
separate argument, so mv --backup --<Tab> normally suggests nothing; with
this plugin it lists mv's options. This works through a _zhc_fallback
completer that the plugin inserts after _complete in the completer style.
If you set that style after the plugin, add _zhc_fallback after
_complete yourself:
zstyle ':completion:*' completer _expand _complete _zhc_fallback _ignored
./install.sh # with oh-my-zsh: symlinks into custom/plugins
Then add zsh-help-complete to plugins=( ... ) in .zshrc and run
exec zsh. Without oh-my-zsh, install.sh appends a source line to the end
of .zshrc (it must come after compinit).
Plugin managers (load it after compinit):
zinit light tanaydin/zsh-help-complete # zinit
antidote bundle tanaydin/zsh-help-complete # antidote (or add to .zsh_plugins.txt)
Manually:
git clone https://github.com/tanaydin/zsh-help-complete ~/.zsh/zsh-help-complete
echo "source ~/.zsh/zsh-help-complete/zsh-help-complete.plugin.zsh" >> ~/.zshrc
The context is :zhc:cmd:<command name>: :zhc:* applies to every command,
:zhc:cmd:mytool to one.
zstyle ':zhc:*' ask no # don't ask before the first run (see Safety)
zstyle ':zhc:*' decisions-file ~/.config/zsh-help-complete/decisions # (default)
zstyle ':zhc:*' script-check no # run scripts even without signs of --help handling
zstyle ':zhc:*' sandbox yes # run help read-only, without network
zstyle ':zhc:*' help-options --help # help flags to try (default: only --help)
zstyle ':zhc:cmd:mytool' help-options --help -hh # also try -hh for mytool
zstyle ':zhc:*' timeout 5 # seconds allowed per help call
zstyle ':zhc:*' denylist rm dd mkfs shutdown # replaces the default list
zstyle ':zhc:*' denylist rm 'mytool deploy' # entries may name a subcommand
zstyle ':zhc:*' allow-paths no # never run executables given by path
zstyle ':zhc:*' subcommands no # no subcommand names; "tool sub --" uses "tool --help"
zstyle ':zhc:*' fallback no # leave commands with their own completion alone
zstyle ':zhc:*' ignore '--foo*' # option patterns to drop (default none)
zstyle ':zhc:*' cache no # don't keep help output in files
zstyle ':zhc:*' cache-dir ~/.cache/zsh-help-complete # where it is kept (default)
zstyle ':zhc:*' enabled no # disable
With several help flags, each is tried in turn and the output with the most option lines wins. If one times out, the remaining flags are skipped.
The help output is kept in files under ~/.cache/zsh-help-complete
($XDG_CACHE_HOME is respected), so each command is run only once, not once
per shell. A file is used only while the executable is unchanged (same
mtime, size and inode; symlinks are followed) and the help-options and
timeout settings are the same, so an updated tool is re-read on the next
Tab, without restarting the shell. A help call that timed out is not kept.
The GIF above is recorded with vhs
(needs vhs and ttyd): run vhs assets/demo.tape from the repo root.
zsh -f tests/run.zsh
Drives a real interactive zsh via zpty. Using fake commands, it checks the
first-run question (each answer, remembered answers, asking again when the
program changed, zhc allow, non-option words never asking), the sandbox
(when bwrap or sandbox-exec works), option values (choices in three
formats, directories, files, skipping a value), subcommand names (aliases,
wrapped descriptions, nesting, argparse), the script check (shell scripts without signs
of --help handling, scripts by path, shell script subcommands not being
run, argparse subcommands), malformed help lines (including one sharing a
name with a real option), --color[=WHEN], completing an option after
another, executables given by path, help flags, subcommands (nested,
unlisted words, denylisted subcommands), the styles, stepping in when a
command's own completion finds nothing (and staying out when it works),
re-reading an updated tool in the same shell, the file cache, a hanging
--help, the denylist, and the fallback to file completion.