complgen: Generate {bash,fish,zsh} completions from a single EBNF-like grammar

2023/07/06
This article was written by an AI 🤖. The original article can be found here. If you want to learn more about how this works, check out our repo.

complgen is a powerful tool that allows developers to generate completion scripts for major shells such as bash, fish, and zsh from a single, concise EBNF-like grammar. This tool compiles the grammar into a standalone shell script that can be distributed independently. Additionally, complgen can also produce completions directly on stdout, making it ideal for interactive shells.

To use complgen, there are two main approaches:

  1. Generating standalone completion scripts for bash, fish, or zsh.
  2. Generating completions on stdout by compiling the grammar "just-in-time".

The just-in-time mode is designed to integrate with shells, providing completions directly from grammars without the need for compilation and sourcing completion shell script files.

To integrate complgen with your shell, follow these instructions:

**Bash Integration:**Assuming your .usage files are stored in the ~/.config/complgen directory, add the following line to your ~/.bashrc file:

source ~/.config/complgen/bash-completions.sh

**Fish Integration:**Assuming your .usage files are stored in the ~/.config/complgen directory, add the following line to your ~/.config/fish/config.fish file:

source ~/.config/complgen/fish-completions.fish

**Zsh Integration:**Assuming your .usage files are stored in the ~/.config/complgen directory, add the following line to your ~/.zshrc file:

source ~/.config/complgen/zsh-completions.zsh

To install complgen, use the following command:

npm install -g complgen

For more information on the syntax and usage of complgen, refer to the examples provided in the subdirectory. You can also generate a grammar skeleton quickly by piping through the scrape subcommand.

complgen is a valuable tool for developers who want to streamline their shell completions and improve their productivity. With its ability to generate completions from a single grammar, complgen simplifies the process of creating and distributing completion scripts across different shells. Stay ahead of the game by incorporating complgen into your development workflow.