Skip to content

The Deska blog

Shell Completions for Your CLI

Master shell completions for your CLI to improve developer experience. Learn how to implement and manage completion scripts for Bash, Zsh, and Fish.

11 min read

Building a command line interface is often the first step in creating a developer tool, but the difference between a functional utility and a professional product lies in the details. One of the most critical aspects of this refinement is shell completions for your CLI, which allow users to discover commands and arguments dynamically by pressing the tab key. Without completions, users are forced to memorize syntax or constantly refer to help menus. When you implement robust tab completion, you reduce cognitive load and prevent syntax errors before the user even presses enter.

The Mechanics of Shell Completions

A shell completion script is essentially a set of instructions that tells the user shell how to respond when a specific command is followed by a tab character. The process differs significantly between shells, as each has its own internal logic for parsing command buffers.

Bash typically uses the complete and compgen builtins. When you trigger completion, Bash runs a function that populates a list of possible matches based on the current word being typed. Zsh offers a more sophisticated system through its compsys module, providing better visual menus and descriptions for arguments. Fish is arguably the most user friendly out of the box, as it parses man pages and uses a simple complete command with descriptive flags to define behavior.

Implementing Completions for Different Shells

To provide a seamless experience, you must target the most common environments used by developers. Each environment requires a specific file format and installation path.

Bash Completions

In Bash, you define a completion function and then register it. The function typically uses the COMPREPLY array to store results. You might see a structure like this:

_mytool_completions() {
  local cur=${COMP_WORDS[COMP_CWORD]}
  COMPREPLY=( $(compgen -W "build test deploy" -- "$cur") )
}
complete -F _mytool_completions mytool
}

This script must be sourced in the user profile or placed in /etc/bash_completion.d/ to be active in new sessions.

Zsh Completions

Zsh completions are often stored in files named with a leading underscore, such as _mytool. These files utilize the #compdef directive at the top. The _arguments helper function is the standard way to handle complex flags and subcommands. It allows you to specify whether a flag requires a file, a directory, or a custom list of strings.

Fish Completions

Fish uses a declarative approach. Instead of writing a complex shell function, you call the complete command multiple times to define your CLI surface. For example, complete -c mytool -a build -d 'Build the project' defines an action and a description. Fish handles the filtering and display automatically.

Integrating Completions into Modern Workspaces

As workflows move toward graphical environments that integrate terminals, the way we interact with completions is changing. Deska provides a unique environment for this type of CLI work. It is a free desktop app for Mac, Windows, and Linux that uses an infinite canvas workspace. Within this canvas, you can place multiple terminals side by side to test how your completion scripts behave in different shells simultaneously.

Because Deska is local-first, all your configuration files and shell sessions remain on your machine. You can open a code editor panel next to your terminal to iterate on your completion logic. This layout makes it easier to verify that your bashrc or zshrc changes are loading correctly without switching windows constantly.

Standard Libraries for CLI Completions

Writing completion scripts by hand is tedious and error prone. Most modern programming languages offer libraries that generate these scripts automatically from your code definition.

  1. Cobra (Go): Perhaps the gold standard for CLI tools. It generates completion scripts for Bash, Zsh, Fish, and PowerShell with a single command.
  2. Click (Python): Provides excellent support for shell completion, though it sometimes requires the user to run a specific command to generate the hook.
  3. Clap (Rust): Offers a derive feature that can produce completion files at build time or via a subcommand.
  4. Oclif (Node.js): Includes plugins specifically designed to manage the installation of completion scripts for the user.

Using these libraries ensures that when you add a new flag to your tool, the completions are updated automatically. This consistency is vital for maintaining a polished developer experience.

Testing and Distribution Strategies

Once your scripts are written, you need a way to get them to the user. There are two common patterns for distribution.

The first is the static approach. You include the completion files in your package distribution, such as a Homebrew formula or a Debian package. The package manager places the files in the correct system directories. The second is the dynamic approach, where your CLI tool includes a command like mytool completion zsh. The user adds source <(mytool completion zsh) to their shell profile. The dynamic approach is often preferred for tools that update frequently, as the completion logic stays in sync with the binary version.

Managing Complex CLI Sessions

When you are developing and testing these scripts, your workspace can become cluttered with various terminal tabs. In Deska, you can use the canvas to organize these sessions. You can have one panel running a Zsh session, another running Bash, and a third running a coding agent to help you debug the shell logic.

If you are away from your workstation, the Deska mobile app allows you to monitor these long running sessions or check the output of a test suite. The mobile app uses a secure relay to pair directly with your desktop, so you do not need to expose any ports or modify your firewall to see your work.

Common Pitfalls in Completion Logic

The most frequent mistake is failing to handle paths correctly. If your CLI accepts a file path as an argument, the completion script should default to the shell's standard file completion. Overriding this with a custom list that does not include files will frustrate users.

Another issue is performance. Shell completions run every time a user hits tab. If your script calls an external API or performs a heavy disk scan to generate suggestions, it will feel sluggish. Always cache results where possible or keep the logic strictly local. If you need help optimizing your scripts, you can ask Deska using the voice or chat assistant to suggest improvements to your shell functions or to help you reorganize your workspace for better productivity.

FAQ

How do I install bash completions on macOS?

MacOS uses an older version of Bash by default. To get modern completion support, you should install the bash-completion package via a package manager. You then need to source the main completion script in your ~/.bash_profile. After this is set up, you can place your specific CLI completion scripts in the bash_completion.d directory.

Why are my Zsh completions not showing up?

Zsh requires the compinit function to be called in your .zshrc file. Additionally, the directory containing your completion script must be included in the fpath variable before compinit is executed. If you add a new script, you might also need to delete the .zcompdump file and restart your shell to force Zsh to rebuild its completion cache.

Can I use completions in a Windows terminal?

Yes, although the implementation depends on the shell you are using. If you use PowerShell, you can use the Register-ArgumentCompleter cmdlet. If you use the Windows Subsystem for Linux, the process is identical to Linux. Deska provides a consistent experience across all these platforms, allowing you to run Linux-style terminals and Windows-native shells within the same workspace.

Enhance Your Workflow

Adding shell completions for your CLI is a high impact way to improve the developer experience of your tool. It turns a static utility into an interactive environment. To explore how to better manage your development tools and AI agents, you can download Deska at /download. Whether you are refining a new CLI or managing complex infrastructure, the flexible canvas and local-first approach provide the stability and visibility required for modern engineering.