ZSH Completion System

VerifiedSafe

Patterns and conventions for writing custom ZSH completion files. Comprehensive guide covering file structure, function registration, and options management.

Sby Skills Guide Bot
DevelopmentIntermediate
3/10/2026
Claude Code
#zsh#shell-completion#dotfiles#cli#compdef

Recommended for

Automated summary

Reference guide for writing and maintaining custom ZSH completion files in a dotfiles repository, covering naming conventions, file structure and `_arguments` patterns.

Strengths

  • Provides a complete, copy-ready file skeleton (the `_toolname` function, `_arguments -C`, `compdef`)
  • Makes the repo's conventions explicit: `<topic>/completion.zsh` files are sourced directly instead of being added to `fpath`
  • Summarizes the built-in completers (`_files`, `_directories`, `_values`, `_describe`) with syntax for options, fixed choices and free-form values
  • Includes a workflow for deriving completions from `--help` output plus a post-edit testing checklist

Limitations

  • Tightly coupled to one specific dotfiles layout; the paths and the loading logic in `zshrc.symlink` do not transfer as-is
  • Does not cover advanced completion features such as caching, `_regex_arguments`, or completions generated dynamically from command output
  • Assumes prior familiarity with `_arguments` syntax, which is only shown through examples
When to use it

Use it when creating or fixing a ZSH completion file for a command-line tool inside this dotfiles repository.

When not to use it

Skip it when installing Homebrew packages, since those ship their completions automatically.

Security analysis

Safe
Quality score84/100

The skill is purely documentation for writing zsh completion files. It references Bash only for reloading the shell, syntax-checking, and reading `--help` output, and Write/Edit for adding completion scripts — all legitimate. There are no destructive commands, no secret/network exfiltration, no base64 or curl|sh patterns, and no instructions to disable safety mechanisms, so listing it carries no meaningful execution risk.

Findings
  • •Declares Bash/Write/Edit, which allow arbitrary command execution and file modification, but the document only prescribes benign commands (`source ~/.zshrc`, `zsh -n <file>`, `<tool> --help`) for testing completions
  • •Writes are scoped to creating `<topic>/completion.zsh` files in a dotfiles repo; no destructive or exfiltration behavior, no obfuscation, no remote payload fetching
How this analysis is made

Examples

New tool completion
Create a completion file for our internal `deploy` CLI with subcommands `init`, `status` and `rollout`, following the repo's `<topic>/completion.zsh` convention.
Fix an existing completion
The `--format` option of our tool doesn't suggest any values when I hit TAB. Read claude/completion.zsh and fix it so it completes json, yaml and text.
Nested subcommands
Add nested subcommand completion for a docker-like tool where `tool service <TAB>` lists start/stop/restart and each of those suggests the service name.

name: completions description: ZSH completion system patterns and conventions. Use when implementing custom completion handling, writing completion files, or working with zsh autocomplete. Do not use when installing packages from homebrew, since that typically installs completions automatically. allowed-tools: [Read, Write, Edit, Bash, Grep, Glob]

Completions

Guide for writing and managing custom ZSH completion files in this dotfiles repository.

When to Use

  • Implementing custom completions for CLI tools
  • Fixing or updating existing completion files
  • Understanding how completions are loaded in this repo

Do NOT use when installing homebrew packages - they handle completions automatically.

Repository Conventions

File Naming

Completion files in this repo use: <topic>/completion.zsh

Example: claude/completion.zsh

Loading Process

From zsh/zshrc.symlink:

  1. compinit is called to initialize the completion system
  2. All */completion.zsh files are sourced
  3. Files are sourced directly, NOT added to fpath

File Structure

#!/usr/bin/env zsh

# Define completion function
_toolname() {
  local line state

  # Define options
  local -a options=(
    '-h[Display help]'
    '--help[Display help]'
  )

  # Define subcommands
  local -a subcommands=(
    'command:Description'
  )

  _arguments -C \
    "${options[@]}" \
    '1: :->command' \
    '*::arg:->args'

  case $state in
    command)
      _describe -t commands 'tool command' subcommands
      ;;
    args)
      # Handle subcommand args
      ;;
  esac
}

# Register completion
compdef _toolname toolname

Key Patterns

Registration

Use compdef at the end of the file:

compdef _toolname toolname

NOT the #compdef directive (that's for files in fpath).

Options Format

local -a options=(
  '-h[Display help]'
  '--help[Display help]'
  '--flag[Description]'
  '--option[Description]:value:'
  '--file[Description]:file:_files'
  '--dir[Description]:directory:_directories'
)

Subcommands

local -a subcommands=(
  'command:Description'
  'subcommand:What it does'
)

_describe -t commands 'tool command' subcommands

Argument Handling

_arguments -C \
  "${options[@]}" \
  '1: :->command' \
  '*::arg:->args'

case $state in
  command)
    _describe -t commands 'tool command' subcommands
    ;;
  args)
    case ${line[1]} in
      subcommand)
        _arguments \
          '--subcommand-option[Description]' \
          '1:arg:'
        ;;
    esac
    ;;
esac

Nested Subcommands

For tools like git or docker with deep command hierarchies:

_tool_subcommand() {
  local line state

  local -a sub_subcommands=(
    'action:Description'
  )

  _arguments -C \
    '1: :->command' \
    '*::arg:->args'

  case $state in
    command)
      _describe -t commands 'tool subcommand command' sub_subcommands
      ;;
  esac
}

Common Completers

Built-in zsh completion functions:

  • _files - File paths
  • _directories - Directory paths
  • _values - Predefined values
  • _describe - Command descriptions
  • _arguments - Argument parsing

File Completion

'--config[Config file]:file:_files'

Choice Completion

'--format[Output format]:format:(json yaml text)'

Multiple Values

'--env[Environment variables]:env:'  # Free form
'--scope[Scope]:scope:(local user project)'  # Fixed choices

Testing

After creating/modifying a completion file:

  1. Reload shell: source ~/.zshrc
  2. Test completion: tool <TAB>
  3. Test subcommands: tool subcommand <TAB>
  4. Test options: tool --<TAB>

Generating Completions

Steps to create completions for a new tool:

  1. Run tool --help to see main options
  2. Run tool subcommand --help for each subcommand
  3. Create <topic>/completion.zsh
  4. Define _toolname() function with all discovered options
  5. Register with compdef _toolname toolname
  6. Test thoroughly

Common Issues

"command not found: _arguments"

The completion function is being executed instead of sourced. Ensure:

  • File is named completion.zsh (not completions.zsh)
  • Using compdef (not #compdef)
  • File is in a topic directory that gets sourced

"can only be called from completion function"

Using #compdef directive when file is sourced directly. Use compdef registration instead.

Completions Not Loading

  1. Check file naming: */completion.zsh
  2. Verify file is being sourced: echo $ZSH/**/*.zsh | grep completion
  3. Check for syntax errors: zsh -n path/to/completion.zsh

Examples

See existing completion files:

  • claude/completion.zsh - Complex multi-level subcommands
  • gcloud/completions.zsh - Third-party completion sourcing

From the file to a first result

The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.

View the source and supporting files

This download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.

1. Download the file

Download SKILL.md

From the root of a test project, save the file as .skill-downloads/zsh-completion-system-mmk3nhz5/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.

Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.

mkdir -p '.skill-downloads/zsh-completion-system-mmk3nhz5' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/zsh-completion-system-mmk3nhz5/download' --output '.skill-downloads/zsh-completion-system-mmk3nhz5/SKILL.md.part' && mv '.skill-downloads/zsh-completion-system-mmk3nhz5/SKILL.md.part' '.skill-downloads/zsh-completion-system-mmk3nhz5/SKILL.md'

2. Check prerequisites

Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.

No recognized prerequisites section in this file. This does not mean it is self-contained: libraries, tools and access requirements may be mentioned elsewhere in the content or source repository.

The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.

3. Load the skill

After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.

Claude Code
test -f '.skill-downloads/zsh-completion-system-mmk3nhz5/SKILL.md' && test -s '.skill-downloads/zsh-completion-system-mmk3nhz5/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/completions' && cp -R '.skill-downloads/zsh-completion-system-mmk3nhz5' '.claude/skills/completions'

OpenSkills

OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.

Official OpenSkills installation
openskills install './.skill-downloads/zsh-completion-system-mmk3nhz5'

openskills read 'zsh-completion-system-mmk3nhz5'

Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.

For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.

openskills sync

Manual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.

4. Try a first task

Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:

Read .skill-downloads/zsh-completion-system-mmk3nhz5/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.

Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.

Related skills