Tab
Shell autocompletions are largely missing in the JavaScript CLI ecosystem. tab provides a simple API for adding autocompletions to any JavaScript CLI tool.
Additionally, tab supports autocompletions for pnpm, npm, yarn, and bun.
Modern CLI libraries like Gunshi include tab completion natively in their core.
As CLI tooling authors, if we can spare our users a second or two by not checking documentation or writing the -h flag, we’re doing them a huge favor. The unconscious mind loves hitting the [TAB] key and always expects feedback. When nothing happens, it breaks the user’s flow - a frustration apparent across the whole JavaScript CLI tooling ecosystem.
tab solves this complexity by providing autocompletions that work consistently across zsh, bash, fish, and powershell.
Installation
Section titled “Installation”npm install @bomb.sh/tab# orpnpm add @bomb.sh/tab# oryarn add @bomb.sh/tab# orbun add @bomb.sh/tabQuick Start
Section titled “Quick Start”Add autocompletions to your CLI tool:
import t from '@bomb.sh/tab';
// Define your CLI structureconst devCmd = t.command('dev', 'Start development server');devCmd.option('port', 'Specify port', (complete) => { complete('3000', 'Development port'); complete('8080', 'Production port');});
// Handle completion requestsif (process.argv[2] === 'complete') { const shell = process.argv[3]; if (shell === '--') { const args = process.argv.slice(4); t.parse(args); } else { t.setup('my-cli', 'node my-cli.js', shell); }}Test your completions:
node my-cli.js complete -- dev --port=<TAB># Output: --port=3000 Development port# --port=8080 Production portInstall for users:
# One-time setupsource <(my-cli complete zsh)
# Permanent setupmy-cli complete zsh > ~/.my-cli-completion.zshecho 'source ~/.my-cli-completion.zsh' >> ~/.zshrcPackage Manager Completions
Section titled “Package Manager Completions”As mentioned earlier, tab provides completions for package managers as well:
# Generate and install completion scriptsnpx @bomb.sh/tab pnpm zsh > ~/.pnpm-completion.zsh && echo 'source ~/.pnpm-completion.zsh' >> ~/.zshrcnpx @bomb.sh/tab npm bash > ~/.npm-completion.bash && echo 'source ~/.npm-completion.bash' >> ~/.bashrcnpx @bomb.sh/tab yarn fish > ~/.config/fish/completions/yarn.fishnpx @bomb.sh/tab bun powershell > ~/.bun-completion.ps1 && echo '. ~/.bun-completion.ps1' >> $PROFILEExample in action:
pnpm install --reporter=<TAB># Shows: append-only, default, ndjson, silent
yarn add --emoji=<TAB># Shows: true, falseFramework Adapters
Section titled “Framework Adapters”tab provides adapters for popular JavaScript CLI frameworks.
CAC Integration
Section titled “CAC Integration”import cac from 'cac';import tab from '@bomb.sh/tab/cac';
const cli = cac('my-cli');
// Define your CLIcli .command('dev', 'Start dev server') .option('--port <port>', 'Specify port') .option('--host <host>', 'Specify host');
// Initialize tab completionsconst completion = await tab(cli);
// Add custom completions for option valuesconst devCommand = completion.commands.get('dev');const portOption = devCommand?.options.get('port');if (portOption) { portOption.handler = (complete) => { complete('3000', 'Development port'); complete('8080', 'Production port'); };}
cli.parse();Citty Integration
Section titled “Citty Integration”import { defineCommand, createMain } from 'citty';import tab from '@bomb.sh/tab/citty';
const main = defineCommand({ meta: { name: 'my-cli', description: 'My CLI tool' }, subCommands: { dev: defineCommand({ meta: { name: 'dev', description: 'Start dev server' }, args: { port: { type: 'string', description: 'Specify port' }, host: { type: 'string', description: 'Specify host' }, }, }), },});
// Initialize tab completionsconst completion = await tab(main);
// Add custom completionsconst devCommand = completion.commands.get('dev');const portOption = devCommand?.options.get('port');if (portOption) { portOption.handler = (complete) => { complete('3000', 'Development port'); complete('8080', 'Production port'); };}
const cli = createMain(main);cli();Commander.js Integration
Section titled “Commander.js Integration”import { Command } from 'commander';import tab from '@bomb.sh/tab/commander';
const program = new Command('my-cli');program.version('1.0.0');
// Define commandsprogram .command('serve') .description('Start the server') .option('-p, --port <number>', 'port to use', '3000') .option('-H, --host <host>', 'host to use', 'localhost') .action((options) => { console.log('Starting server...'); });
// Initialize tab completionsconst completion = tab(program);
// Add custom completionsconst serveCommand = completion.commands.get('serve');const portOption = serveCommand?.options.get('port');if (portOption) { portOption.handler = (complete) => { complete('3000', 'Default port'); complete('8080', 'Alternative port'); };}
program.parse();How It Works
Section titled “How It Works”tab uses a standardized completion protocol that any CLI can implement:
# Generate shell completion scriptmy-cli complete zsh
# Parse completion request (called by shell)my-cli complete -- install --port=""Output Format:
--port=3000 Development port--port=8080 Production port:4Contributing
Section titled “Contributing”We welcome contributions! tab’s architecture makes it easy to add support for new package managers or CLI frameworks.
Acknowledgments
Section titled “Acknowledgments”tab was inspired by the great Cobra project, which set the standard for CLI tooling in the Go ecosystem.
Adoption Support
Section titled “Adoption Support”We want to make it as easy as possible for the JS ecosystem to enjoy great autocompletions.
We at Thundraa would be happy to help any open source CLI utility adopt tab.
If you maintain a CLI and would like autocompletions set up for your users, just drop the details in our Adopting tab discussion.
We’ll gladly help and even open a PR to get you started.