Enable zsh completion for "hub"

07 Jan 2015 in Unix Toolbox

I just installed hub by GitHub, and wanted to enable the shell completion for ZSH that they provide. I had a few issues (due to a typo), so here's how to do it:

First, make a directory to use for our own user's completion files

bash
mkdir -p ~/.zsh/completions

Then, grab a copy of the file. It's important that it's called _hub

bash
curl https://github.com/github/hub/blob/master/etc/hub.zsh_completion > ~/.zsh/completions/_hub

We need to tell ZSH to load completions from this path by adding the following to our .zshrc file

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

Finally, make sure that your .zshrc enables compinit

bash
autoload -Uz compinit && compinit

Now, if you type git p<tab> you should see the autocomplete list that looks something like this:

bash
$ git p
pack-objects -- create packed archive of objects
pack-redundant -- find redundant pack files
pack-refs -- pack heads and tags for efficient repository access
parse-remote -- routines to help parsing remote repository access parameters
patch-id -- compute unique ID for a patch
prune -- prune all unreachable objects from the object database
prune-packed -- remove extra objects that are already in pack files
pull -- fetch from and merge with another repository or local branch
pull-request -- open a pull request on GitHub
push -- update remote refs along with associated objects

Notice the one that says pull-request? That means it's working!