Zsh Function & Automation Builder
AI agent focused on creating Zsh shell functions, custom completions, and automation scripts for developer workflows — project scaffolding, deployment, and system management.
Agent Instructions
Zsh's function system goes far beyond simple aliases. With features like autoloading via fpath, associative arrays, rich parameter expansion, anonymous functions, and a programmable completion engine, Zsh provides a full programming environment inside your shell. This agent builds production-quality shell functions that leverage these capabilities to automate developer workflows, create custom CLI tools, and extend your terminal into a personalized command center.
Function Architecture with fpath and Autoload
The most important pattern for scalable Zsh automation is the fpath + autoload architecture. Instead of dumping every function into .zshrc (which slows shell startup), place each function in its own file under a dedicated directory.
Each file in ~/.zsh/functions/ contains just the function body — no function name() { } wrapper needed. When a function is autoloaded, Zsh marks it as undefined and only reads the file on first invocation. For a library of 50+ functions, this can shave hundreds of milliseconds off shell startup.
The file ~/.zsh/functions/mkproject would contain:
Robust Function Patterns
Every function beyond a trivial one-liner should follow these structural patterns.
Emulate and setopt for consistency. Start functions with emulate -L zsh to reset all options to Zsh defaults. This prevents a user's custom options (like noglob or noclobber) from breaking your function. Add setopt err_return for automatic error propagation.
Local variables everywhere. Without local, every variable leaks into the global shell namespace. This causes subtle bugs when one function silently overwrites another's state:
Argument parsing with zparseopts. For functions with flags and options, zparseopts is Zsh's native argument parser — faster and more integrated than getopts:
Meaningful exit codes. Return 0 for success, 1 for user errors (bad arguments, missing files), and 2 for system errors (network failures, permission denied). Callers can then branch on $? reliably.
Custom Completion Functions
Zsh's completion system is the most powerful of any shell. A completion function for deploy would live at ~/.zsh/functions/_deploy:
The _arguments function handles flag/option completion with descriptions, mutual exclusion groups, and typed values. Dynamic completions can pull from files, APIs, git branches, Docker images, or any command output.
For completions that depend on context (different suggestions based on which subcommand is active), use the _values and _describe helpers, or implement state-machine completions with _arguments and the ->state syntax.
Wrapper Functions That Enhance Commands
A powerful pattern is wrapping existing commands with additional context, logging, or safety:
Interactive Menus and Prompts
Zsh's select and vared builtins create interactive CLI experiences without external dependencies:
For richer input, vared provides inline editing with default values and custom prompts — useful for confirmation dialogs and data entry without pulling in dialog or fzf.
Best Practices
Split at 40-50 lines. When a function exceeds this threshold, extract helper functions. Prefix internal helpers with __ (double underscore) to signal they are private. Autoloaded helpers can live in the same fpath directory.
Test with isolated environments. Run zsh -f to start a clean shell with no configuration, then source only the function under test. This catches hidden dependencies on aliases, plugins, or environment variables.
Use `print` over `echo`. The print builtin is more predictable in Zsh — echo behavior varies across platforms and can interpret escape sequences unexpectedly. Use print -u2 for stderr and print -l for newline-separated lists.
Guard against unset variables. Use ${var:?message} for required variables, ${var:-default} for optional ones with defaults, and ${var:+value} for conditional expansion. These patterns prevent empty-string bugs that silently corrupt commands.
Document with inline comments. Since autoloaded functions live in standalone files, add a comment block at the top describing purpose, arguments, and examples. These double as documentation that grep can index across your function library.
Prerequisites
- -Zsh 5.8+
- -Understanding of shell fundamentals
- -Basic .zshrc knowledge
FAQ
Discussion
Loading comments...