# How to Generate CLI Shell Completions for ai-memory

> Learn how to generate CLI shell completions for ai-memory using the clap-complete crate and a hidden subcommand. Easily set up custom completion scripts for your terminal.

- Repository: [Fabio Akita/ai-memory](https://github.com/akitaonrails/ai-memory)
- Tags: how-to-guide
- Published: 2026-09-11

---

**The ai-memory CLI uses the clap-complete crate to generate shell-specific completion scripts via a hidden `completions` subcommand that outputs definitions to stdout, ready for redirection to your shell's completion directory.**

The `akitaonrails/ai-memory` repository provides a Rust-based command-line interface built with the Clap argument parser. Generating **CLI shell completions for ai-memory** requires invoking the built-in generator that produces tab-completion scripts for Bash, Zsh, Fish, and PowerShell directly from the live code structure.

## How Shell Completions Work in ai-memory

The tool leverages **clap** (version 4) paired with the **clap_complete** crate to create completion definitions dynamically. Rather than maintaining static shell scripts, the binary introspects its own argument structure at runtime to guarantee accuracy.

### The Completion Generator Implementation

In [`crates/ai-memory-cli/src/main.rs`](https://github.com/akitaonrails/ai-memory/blob/main/crates/ai-memory-cli/src/main.rs), the application constructs a `clap::Command` through a function typically named `build_cli()`. The code registers a hidden subcommand—commonly called `completions`—that accepts a shell argument and invokes `clap_complete::generate` to write the script to the provided writer. This ensures completions always match the current binary version.

### Dependency Configuration

The completion functionality depends on two crates declared in [`crates/ai-memory-cli/Cargo.toml`](https://github.com/akitaonrails/ai-memory/blob/main/crates/ai-memory-cli/Cargo.toml):

- `clap = "4"` for argument parsing
- `clap_complete = "4"` for script generation

## Generate Completions for Your Shell

Run the `completions` subcommand followed by your shell name to output the definitions to stdout.

### Bash

Install system-wide:

```bash
ai-memory completions bash > /etc/bash_completion.d/ai-memory

```

Or append to user-local configuration:

```bash
ai-memory completions bash >> ~/.bash_completion

```

### Zsh

Install to the first directory in your `$fpath`:

```bash
ai-memory completions zsh > "${fpath[1]}/_ai-memory"

```

For system-wide installation:

```bash
sudo sh -c 'ai-memory completions zsh > /usr/local/share/zsh/site-functions/_ai-memory'

```

### Fish

```bash
ai-memory completions fish > ~/.config/fish/completions/ai-memory.fish

```

### PowerShell

Append to your profile:

```powershell
ai-memory completions powershell >> $PROFILE.CurrentUserAllHosts

```

## Verifying the Implementation Source

To confirm completion availability in your build, check [`crates/ai-memory-cli/src/main.rs`](https://github.com/akitaonrails/ai-memory/blob/main/crates/ai-memory-cli/src/main.rs) for the subcommand registration. If the project separates CLI construction into a dedicated module, the `build_cli()` function in [`crates/ai-memory-cli/src/cli.rs`](https://github.com/akitaonrails/ai-memory/blob/main/crates/ai-memory-cli/src/cli.rs) defines the argument structure used by the generator.

## Summary

- The ai-memory CLI includes a `completions` subcommand powered by clap-complete
- Scripts are generated dynamically from the live `clap::Command` definition in [`crates/ai-memory-cli/src/main.rs`](https://github.com/akitaonrails/ai-memory/blob/main/crates/ai-memory-cli/src/main.rs)
- Supported shells include Bash, Zsh, Fish, and PowerShell
- Output is written to stdout for flexible installation via shell redirection
- Dependencies are declared in [`crates/ai-memory-cli/Cargo.toml`](https://github.com/akitaonrails/ai-memory/blob/main/crates/ai-memory-cli/Cargo.toml)

## Frequently Asked Questions

### What shells are supported by ai-memory completions?

The ai-memory CLI supports generating completion scripts for Bash, Zsh, Fish, and PowerShell through the `clap_complete` crate integration. Run `ai-memory completions --help` to verify available shell options in your specific build.

### Do I need to rebuild ai-memory to get updated completions?

No manual rebuilding is required for installation, but you must regenerate scripts after upgrading the binary. Since completions are derived from the live `clap::Command` structure defined in [`crates/ai-memory-cli/src/main.rs`](https://github.com/akitaonrails/ai-memory/blob/main/crates/ai-memory-cli/src/main.rs), simply run the `completions` subcommand again after updating ai-memory to refresh your shell definitions.

### Where should I save the generated completion files?

Location depends on your shell and operating system. Bash typically uses `/etc/bash_completion.d/` or `~/.bash_completion`, Zsh uses paths in `$fpath` such as `/usr/local/share/zsh/site-functions/`, Fish uses `~/.config/fish/completions/`, and PowerShell uses the path stored in `$PROFILE.CurrentUserAllHosts`.

### Can I generate completions without installing them?

Yes. Running `ai-memory completions <shell>` without redirection prints the script to stdout, allowing you to inspect, modify, or manually copy the output. This is useful for testing changes or integrating with custom dotfiles management systems.