DEV Community

Cover image for Adding Zsh Completion for `gh copilot`

Adding Zsh Completion for `gh copilot`

You are not alone if you are using the gh copilot CLI and have noticed the lack of Zsh completions.

To address this issue, I (err.. GPT, actually) created a completion script to enhance the command-line experience.

Below is a quick guide on setting it up, including a tip on enabling autocompletion for aliases.

Add a few aliases (optional)

Somewhere on your ~/.zshrc, add:

# GitHub Copilot
# Use Copilot with the GitHub CLI to get suggestions and explanations for the command line
# https://docs.github.com/en/copilot/using-github-copilot/using-github-copilot-in-the-command-line
if (( $+commands[gh] )); then
  # GitHub Copilot
  alias ghcp='gh copilot'

  # Explain a command
  alias cpe='ghcp explain'

  # Suggest a command
  alias cps='ghcp suggest'
fi
Enter fullscreen mode Exit fullscreen mode

After reloading the shell, you should be able to run ghcp --help:

$ ghcp --help
Your AI command line copilot.

Usage:
  copilot [command]

Examples:

$ gh copilot suggest "Install git"
$ gh copilot explain "traceroute github.com"


Available Commands:
  alias       Generate shell-specific aliases for convenience
  config      Configure options
  explain     Explain a command
  suggest     Suggest a command

Flags:
  -h, --help              help for copilot
      --hostname string   The GitHub host to use for authentication
  -v, --version           version for copilot

Use "copilot [command] --help" for more information about a command.
Enter fullscreen mode Exit fullscreen mode

However, when running ghcp <TAB>, nothing happens...

Yet!

Create the Completion Script

First, create a new file for the completion script and name it _ghcp. Place the following content inside:

#compdef ghcp

local context state line
typeset -A opt_args

_arguments -C \
  '-h[help]' \
  '--help[help]' \
  '--hostname=[The GitHub host to use for authentication]:hostname:' \
  '-v[version for copilot]' \
  '--version[version for copilot]' \
  '1:command:->command' \
  '*::args:->args'

case $state in
  command)
    local -a commands
    commands=(
      'alias:Generate shell-specific aliases for convenience'
      'config:Configure options'
      'explain:Explain a command'
      'suggest:Suggest a command'
    )
    _describe -t commands 'copilot command' commands
    ;;
  args)
    case $words[2] in
      alias)
        # No additional arguments for 'alias'
        ;;
      config)
        # No additional arguments for 'config'
        ;;
      explain)
        _message 'Provide the command you want to explain'
        ;;
      suggest)
        _message 'Provide a description of what you want to do'
        ;;
    esac
    ;;
esac

# Associate the completion script with the alias
compdef _ghcp ghcp
Enter fullscreen mode Exit fullscreen mode

Install the Completion Script

Next, you must place the _ghcp file in a directory that's part of your $fpath. Here's how you can do it:

1. Create a Completions Directory

If you don't already have a custom directory for Zsh completions, create one:

$ mkdir -p ~/.zsh/completions

2. Move the Script

Move or copy the _ghcp file into this directory:

$ mv _ghcp ~/.zsh/completions/

3. Update Your $fpath

Add the completions directory to your $fpath by appending this line to your ~/.zshrc:

$ fpath=(~/.zsh/completions $fpath)

4. Ensure the Script Is Executable

Make the completion script executable:

$ chmod +x ~/.zsh/completions/_ghcp

5. Enable Alias Completion

If you use aliases for gh copilot (like ghcp), Zsh must be configured to handle completions for aliases. Add the following line to your ~/.zshrc:

$ setopt complete_aliases

Alternatively, you can explicitly associate the completion script with your alias by adding:

$ compdef _ghcp ghcp

6. Reload Zsh Configuration

Apply the changes by sourcing your .zshrc or restarting your terminal:

$ source ~/.zshrc

Is that so?

Well, let's try typing ghcp <TAB> and.. voila!

You should see the available subcommands and options auto-complete for you.

Summary

Having shell completions can significantly speed up your workflow by reducing the need to remember every command and option.

Enabling alias completion ensures that your custom shortcuts benefit from the same convenience.

That's all, folks!

Top comments (0)