Enable zsh completion for “hub”

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

mkdir -p ~/.zsh/completions

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

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

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

Finally, make sure that your .zshrc enables compinit

autoload -Uz compinit && compinit

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

$ 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!

Michael is a polyglot software engineer, committed to reducing complexity in systems and making them more predictable. Working with a variety of languages and tools, he shares his technical expertise to audiences all around the world at user groups and conferences. You can follow @mheap on Twitter

Thoughts on this post

Leave a comment?

Leave a Reply