gh-completion
Enable shell autocompletion for the GitHub CLI
TLDR
Print a completion script
Append the gh completion script to ~/.bashrc
Append the gh completion script to ~/.zshrc
Display the subcommand help
SYNOPSIS
gh completion shell
Commonly used by sourcing the output:
eval "$(gh completion shell)"
Or for persistent system-wide installation (requires sudo):
gh completion shell | sudo tee /etc/shell_completion.d/gh
PARAMETERS
shell
Specifies the target shell for which to generate the completion script. Supported values typically include bash, zsh, and fish.
DESCRIPTION
The term gh-completion refers to the shell completion functionality provided by the GitHub CLI (gh). It is not a standalone command but rather the output or feature generated by the gh completion subcommand. When sourced into a user's shell configuration (e.g., .bashrc, .zshrc, .profile), gh-completion enables intelligent tab completion for gh commands, their subcommands, arguments, flags, and even specific values (like repository names or branch names). This dynamic completion greatly improves user efficiency by reducing typing, preventing errors, and aiding in the discovery of available options. It's an essential utility for anyone frequently using the GitHub CLI.
CAVEATS
gh-completion (i.e., the script generated by gh completion) must be sourced into your shell, not executed directly. Running it directly will only print the script to standard output without enabling completion.
The generated script is shell-specific; ensure you use the correct shell argument for your environment.
A shell restart (or sourcing the configuration file again) is required for changes to take effect after installation.
Requires the gh CLI to be installed and accessible in the system's PATH.
<B>INSTALLATION EXAMPLE</B>
To enable gh completion for Bash, add the following line to your ~/.bashrc file:
eval "$(gh completion bash)"
For Zsh, add the equivalent line to your ~/.zshrc file:
eval "$(gh completion zsh)"
After adding, either restart your shell or run source ~/.bashrc (or source ~/.zshrc) to apply the changes.
HISTORY
The completion subcommand was introduced early in the development of the GitHub CLI (gh) to provide a seamless user experience across various popular shells. As gh evolved, its completion capabilities also improved, offering more intelligent suggestions for dynamic content like repository names, user aliases, and command-specific options. This functionality is a standard and expected feature for many modern command-line tools to enhance usability and productivity.