$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
CLI Toolsadvanced

CLI Autocomplete

Share

Add shell autocompletion to CLI tools

Works with OpenClaude

You are a CLI tool developer. The user wants to add shell autocompletion (bash, zsh, fish) to their command-line application.

What to check first

  • Run echo $SHELL to identify the user's current shell
  • Check if your CLI tool is installed globally or available in PATH via which <tool-name>
  • Verify Node.js version supports process.argv and process.env.COMP_* variables (Node 12+)

Steps

  1. Create a completion script generator function that reads COMP_WORDS, COMP_CWORD, and COMP_LINE environment variables (bash) or equivalent (zsh/fish)
  2. Parse the current command line input to determine what completions to suggest based on subcommands, flags, and arguments
  3. Implement a --completion or completion subcommand that outputs completion options line-by-line to stdout
  4. Register completion script in shell config files: ~/.bashrc (bash), ~/.zshrc (zsh), or ~/.config/fish/config.fish (fish)
  5. Use source directive or eval to load completion function on shell startup
  6. Test with TAB key by typing partial command and pressing TAB twice
  7. For npm packages, add postinstall script to automatically register completions or use completion libraries like oclif or yargs-completion

Code

#!/usr/bin/env node

const fs = require('fs');
const path = require('path');

// Main CLI with completion support
const cli = {
  commands: ['start', 'stop', 'status', 'deploy', 'logs'],
  flags: ['--help', '--version', '--config', '--verbose', '--dry-run'],
  
  getCompletions(line) {
    const parts = line.trim().split(/\s+/);
    const current = parts[parts.length - 1] || '';
    const previous = parts[parts.length - 2];
    
    // If typing a flag
    if (current.startsWith('-')) {
      return this.flags.filter(f => f.startsWith(current));
    }
    
    // If typing a command
    if (parts.length === 1 || !this.commands.includes(parts[0])) {
      return this.commands.filter(c => c.startsWith(current));
    }
    
    // Context-aware completions based on command
    if (previous === 'deploy') {
      return ['staging', 'production'].filter(e => e.startsWith(current));
    }
    if (previous === '--config') {
      const configDir = path.join(process.env.HOME, '.config');
      try {
        return fs.readdirSync(configDir).filter(f => f.startsWith(current));
      } catch (e) {
        return [];
      }
    }
    
    return [];

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategoryCLI Tools
Difficultyadvanced
Version1.0.0
AuthorClaude Skills Hub
cliautocompleteshell

Install command:

curl -o ~/.claude/skills/cli-autocomplete.md https://claude-skills-hub.vercel.app/skills/cli/cli-autocomplete.md

Related CLI Tools Skills

Other Claude Code skills in the same category — free to download.

Want a CLI Tools skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.