Tech News
← Back to articles

Writing Your Own Simple Tab-Completions for Bash and Zsh

read original related products more articles

The last quality of life feature we will add is the ability to show completion descriptions when tabbing on a complete word:

$ foo apple

For example, the Mill build tool does this so if you’re not sure what a flag or command does, you can press on it to see more details:

Tab-completion is a common way to explore unfamiliar APIs, and just because someone finished writing a flat or command doesn’t mean they aren’t curious about what it does! But while Zsh tab-completion displays descriptions when multiple options match the prefix, and we managed to hack Bash tab-completion to do the same thing, neither displays any information if the word you are tab-completing is already complete.

This behavior can be annoying, if the user wants to see the description, they will need to first:

Delete enough characters to make the token match multiple completions

Press

Visually scan the multiple completions printed to find the word description they care about

Type back in all the missing characters so they can run the command

To solve this, we can hack Bash and Zsh to print tab-completion descriptions even if the token is already a complete word. We do this by checking if the token is a complete word, and if so adding a second "dummy" completion: this makes the tab-completion ambiguous, which cases Bash and Zsh to print out the completions and descriptions for the user to see.

... continue reading