LinuxCommandLibrary

gh-completion

Enable shell autocompletion for the GitHub CLI

TLDR

Print a completion script

$ gh completion [[-s|--shell]] [bash|zsh|fish|powershell]
copy

Append the gh completion script to ~/.bashrc
$ gh completion [[-s|--shell]] bash >> ~/.bashrc
copy

Append the gh completion script to ~/.zshrc
$ gh completion [[-s|--shell]] zsh >> ~/.zshrc
copy

Display the subcommand help
$ gh completion
copy

SYNOPSIS

gh completion [--shell <bash|zsh|fish|powershell>]

PARAMETERS

--shell <string>
    Target shell: bash, zsh, fish, or powershell (default: bash)

-h, --help
    Display help for gh completion

DESCRIPTION

gh completion is a subcommand of the GitHub CLI tool (gh) designed to generate autocompletion scripts for various shells, enhancing user productivity by enabling tab-completion of commands, options, and arguments.

It supports popular shells including Bash, Zsh, Fish, and PowerShell. When invoked, it outputs a script that must be sourced in the shell configuration file (e.g., .bashrc, .zshrc) or installed system-wide.

For example, in Bash: gh completion bash > ~/.gh-completion.bash followed by source ~/.gh-completion.bash. This provides context-aware completions, such as suggesting repositories, issues, or PRs based on gh context.

The command is lightweight, requiring no additional dependencies beyond the GitHub CLI. It's particularly useful for power users managing GitHub workflows via CLI, reducing typing errors and speeding up interactions. Completions dynamically update with gh updates.

CAVEATS

Scripts must be manually sourced or installed (e.g., to /etc/bash_completion.d/); completions unavailable until reload. System-wide install may require sudo. Not persistent across gh upgrades without regeneration.

BASH INSTALLATION

gh completion bash | sudo tee /etc/bash_completion.d/gh >/dev/null
Then source /etc/bash_completion.d/gh or restart shell.

ZSH INSTALLATION

gh completion zsh > ${fpath[1]}/_gh
Requires Zsh completion framework enabled.

FISH INSTALLATION

gh completion fish | sudo tee ~/.config/fish/completions/gh.fish >/dev/null

HISTORY

Introduced in GitHub CLI v0.6.0 (April 2020) with initial Bash/Zsh support; expanded to Fish/PowerShell in v1.0 (2021). Actively maintained as core feature.

SEE ALSO

gh(1), bash-completion(1), zshcompctl(1), fish_config(1)

Copied to clipboard