@carlwr/zsh-lexicon
v0.1.1
Published
Structured zsh reference data parsed from official zsh documentation
Maintainers
Readme
zsh-lexicon TypeScript library/npm package
[!NOTE]
This
README.mdfile is written entirely by me, Carl, a human developer.Details: section
README.mdby a human, code by LLMs
A strongly typed interface to structured zsh knowledge.
The knowledge itself is parsed from the sources of the zsh 5.9 manual in the upstream zsh official repo.
Additionally, the structured knowledge (records) are available in rendered markdown form.
This package/library powers other parts of this project:
- a CLI
- an MCP server
- a VS Code extension
- a live web app with retrieval through natural-language search
Install
npm install @carlwr/zsh-lexicon # or:
pnpm add @carlwr/zsh-lexicon
# Deno:
deno add npm:@carlwr/zsh-lexicon
# (run with `--allow-read` — the vendored data is read at runtime)Usage example
import { loadLexicon } from "@carlwr/zsh-lexicon"
import { resolve } from "@carlwr/zsh-lexicon/resolver"
import { renderRecord } from "@carlwr/zsh-lexicon/render"
const lexicon = loadLexicon()
// resolve() is where a raw string meets the lexicon:
const hit = resolve(lexicon, "option", "NO_AUTO_CD")
if (!hit) throw new Error("not a zsh option")
console.log(hit.record.id) // → autocd
console.log(hit.feedback) // → { kind: "input-negated" }
const { title, mdBody } = renderRecord(lexicon, hit.record)
console.log(`${title}\n\n${mdBody}`)After if (!hit) throw .. above, no further lookups or checks are needed, since after that, the compiler knows hit represents a valid, existing record.
Below we create a custom function that demands a record id within the option category. We then call the function with the hit value from the code block above.
import type { Documented } from "@carlwr/zsh-lexicon"
function optionLink(id: Documented<"option">): string {
return `#option-${id}`
}
optionLink(hit.record.id) // ✓
optionLink("autocd") // ✗ compile error, even with the right stringIn zsh, unset is both a shell option and a builtin so below, both resolve() calls will return a record. The compiler will only let us call optionLink() in the correct way:
const unsetOptionHit = resolve(lexicon, "option" , "unset")
const unsetBuiltinHit = resolve(lexicon, "builtin", "unset")
if (unsetOptionHit) optionLink(unsetOptionHit.record.id) // ✓
if (unsetBuiltinHit) optionLink(unsetBuiltinHit.record.id) // ✗ compile errorSome records cross-reference other records. The type of those cross-references says that the record exists and what category it belongs to. Therefore, below we can call our function with the cross-reference value directly, once we have checked that our call to resolve() indeed returned a record, and that the record has the optional aliasOf field:
const dotglobAlias = resolve(lexicon, "option", "DOT_GLOB")?.record.aliasOf
if (dotglobAlias) optionLink(dotglobAlias.target) // ✓ → "#option-globdots"The library API
Full documentation: https://carlwr.github.io/zsh-lexicon/docs/
This library is:
- rich in types, poor in functions
- pure, static and side-effect-free[^side-effects-caveats]
General structure:
Pieces of documentation live in records.
- e.g. one record for the
AUTO_CDshell option
- e.g. one record for the
Records are organized in categories.
- e.g. the category
optionfor theAUTO_CDrecord
- e.g. the category
Look-up happens through category-specific resolvers.
- e.g. a look-up for both
autocdandNO_auto_cdwill return the record forAUTO_CD - The resolvers encode zsh domain knowledge to make life easier for consumers.
- e.g. a look-up for both
Different categories may have different data shapes on top of some fields that are always shared. This is all encoded in static types.
The category taxonomy is closed, meaning completeness can be enforced by the compiler.
Per-category quirks are generally hidden. Consumers generally won't need to branch on categories.
When loadLexicon() is called, it returns immediately without doing any work. All processing happens only later, as needed. The bundled data is processed the first time it is needed, per-category, and is cached. In other words: behind-the-scenes, everything happens lazily on-demand per-category, with caching.
For non-TS consumers: JSON + schema
These are available as GitHub Release Assets. The data is the same as that provided by the TypeScript library. The JSON Schema encodes the structure, and to some extent, the invariants.
The GitHub Release Assets have SHA-256 checksums and build-provenance attestation which the user can check with:
gh attestation verify <FILE> --repo carlwr/zsh-lexiconTrust surface
- ❌ shell execution
- ❌ subprocess-spawning
- ❌ network
- ❌ environment reads
- ❌ logs
- ❌ telemetry
- ❌ npm install scripts
(Linked items: tests that back up or guard the claim.)
There is a single dependency: @carlwr/typescript-extra (same maintainer as this package), which itself has no dependencies.
Categories
The reference contains some 1300 records sorted into the following categories:
| Category | Examples |
| ---------------------------- | ---------------------------- |
| Builtins | print, setopt, zle |
| Options | AUTO_CD, EXTENDED_GLOB |
| Special parameters | $?, $PWD, $path |
| Special functions | precmd, TRAPINT |
| Complex commands | if, for, case, [[ |
| Reserved words | then, done, coproc |
| Precommand modifiers | noglob, command, exec |
| Conditional operators | -f, -nt, =~ |
| Arithmetic operators | **, <<, ^^ |
| Math functions | sqrt, int, rand48 |
| Redirections | 2>&1, <<<, &> |
| Process substitutions | <(...), >(...), =(...) |
| Parameter-expansion forms | ${name:-word}, ${#spec} |
| Parameter-expansion flags | (U), (j:,:), (@) |
| Parameter-subscript flags | (r), (i), (w) |
| Glob operators | [...], ^x, x~y |
| Glob qualifiers | (.), (/), (N) |
| Glob flags | (#i), (#b) |
| History-expansion components | !!, !-2, :h |
| Prompt escapes | %~, %n, %F |
| Job specs | %1, %+, %- |
| ZLE widgets | accept-line, vi-cmd-mode |
| ZLE keymaps | emacs, viins, vicmd |
| Completion utilities | _arguments, _files |
Node requirement and versioning policy
All major Node.js versions that haven't reach end-of-life are supported. Currently that means Node ≥ 22.
Dropping support for a Node.js line that has reached its end-of-life is a minor release of this package.
README.md by a human, code by LLMs
This README.md file is written entirely by me, Carl, a human developer. Agentic tools have not been allowed to make edits, suggest phrasing or even suggest corrections to its prose, except for spelling fixes. Agentic tab-complete has been disabled. The same goes for any README*.md and any SECURITY.md within this repo.
Other than that, almost everything in this repo is written by LLMs. I-the-human have directed the development, designed the data structures and made the architectural choices. Further, I have closely reviewed (at minimum:) all code related to security surfaces; to the best of my ability.
The models used, at least up until the public 0.1.0 release, were mostly Opus 5.x 1M and GPT-{5.6..6.2}, with dashes of DeepSeek V4 Pro. These models were used through Claude Code, Codex, opencode and occasionally Cursor.
[^side-effects-caveats]: Some bundled files are read lazily, so if Deno is used, --allow-read. There is also one function for writing the bundled data files to a given directory; this function is included to make life easier for bundlers.
