Cod: New Command Line Autocomplete Daemon For Bash and Zsh That Detects --help Usage

Cod command line completion daemon

Cod is a new command line completion daemon written in Go for Bash and Zsh. The tool detects the usage of --help to generate autocompletion for commands that don't support it.

Command-line completion (tab completion / autocompletion) is a common feature among command-line interpreters, in which the program automatically fills in partially typed commands when pressing the completion key, which is usually Tab. By using it, fewer keystrokes are required to access common commands, and it makes it easy to autocomplete commands / filenames with long or difficult to spell names.

The elements that can be completed are not only commands and filenames, but also command arguments, and this is what Cod does. It parses the output of --help for a particular command, and based on that it generates autocompletion for Bash or Zsh shells. Some commands already support autocomplete for arguments (for example ls - type ls --fu and press Tab to autocomplete it to ls --full-time), but some don't and Cod can help in those cases.

Cod supports Zsh on macOS and Linux, and Bash only on Linux. Bash on macOS is not supported because, according to the Cod project description, the Bash version that's bundled with macOS is too old and Cod doesn't support it. There's no mention of Windows in the project description, and there are no Windows binaries so it might not support Windows, but I'm not sure since I don't use Windows.

How Cod works


Demo showing how Cod adds autocompletion to the scrcpy command
Demo terminal recording showing how Cod adds autocompletion to the scrcpy command (which doesn't have autocompletion by default)

When you access the --help of a program / command, Cod detects it and asks if it should learn this command. If you allow it, Cod parses the --help output and uses it to complete arguments for that command in the future.

For special cases in which Cod doesn't detect that you have invoked the help of a command, use the Cod learn subcommand to learn it anyway.

Let's look at a real world example: scrcpy (this can be seen in the demo terminal recording a few lines above). This is a tool to show an Android device's screen on a desktop and control it remotely. The scrcpy command has many options / arguments, like --always-on-top, --record-format, --window-borderless, and more, with no built-in autocomplete support for them.

To get Cod to learn the scrcpy command options / arguments, let's run:

scrcpy --help

When running a command with the --help argument, Cod asks if it should learn that command:

┌──> /usr/bin/scrcpy --help
└─── cod: learn this command? [yn?] > y
cod: learned completions: "--always-on-top" "-b" and 35 more

Type y to allow it to learn this command.

Now that Cod has learned the command arguments, let's give it a try. Type

scrcpy --a

in a terminal and press Tab. The argument should be autocompleted to --always-on-top.

You might also like: Incomplete Path Expansion (Completion) For Bash

Besides the already mentioned learn subcommand, Cod has some other options like update (update a known command), list (list known commands), and more. This is the Cod help / usage screen:

usage: cod []  [ ...]

Shell autocomplete generator based on `--help' texts.

Flags:
  --help     Show context-sensitive help (also try --help-long and --help-man).
  --version  Show application version.

Commands:
  help [...]
  learn ...
  list [...]
  remove ...
  update ...
  init  
  example-config []
  daemon []

Visit the Cod project page for more information on how to use and configure it.

Download Cod



You can download the source and build Cod, or download and install the prebuilt binary (available for both macOS and Linux).

To install the Cod binary on Linux, extract the cod-Linux.tgz archive, open a terminal in the folder where you have extracted the Cod binary, and use this command to install it to /usr/local/bin (make sure this is in your PATH, or install it somewhere else):

sudo install cod /usr/local/bin

You'll also need to add a line to your ~/.bashrc or ~/.zshrc file to source Cod:

  • for Bash: open ~/.bashrc with a text editor and at the end of the file add a new line containing:
source <(cod init $$ bash)

  • for Zsh: open ~/.zshrc with a text editor and at the end of the file add a new line with:
source <(cod init $$ zsh)

Save the file, source the modified ~/.bashrc (use this command: source ~/.bashrc) and ~/.zshrc (use source ~/.zshrc) file or open a new terminal and Cod will be ready to use.