Autocompletion for WP-CLI using (oh-my)-zsh

Last Modified on November 16, 2015

UPDATE: It turns out oh-my-zsh now has its own wp-cli plugin. This makes the process much easier. Edit your .zshrc file and add wp-cli to the plugins directive.

# edit ~/.zshrc in nano
nano ~/.zshrc

# find the plugins=() directive and add wp-cli to the list

Now restart your terminal. This plugin also adds some helpful aliasses. See the full list at the plugin page. Much better!

A lot of advanced WordPress developers know WP-CLI. This tool is a great addition to everyday WordPress development life as it allows you to quickly make changes to WordPress. For example; with a few key-strokes you can update WordPress core, it’s themes and it’s plugins. Meanwhile you’ve made changes to the size of your thumbnails so you need to regenerate thumbnails. This is all possible from the terminal through WP-CLI.

Although the commands are pretty straightforward, it can still be a bit of a pain to remember every single one. After some Googling I found out WP-CLI already has an autocompletion script. Unfortunately, this does not immediately work with oh-my-zsh so I searched some more and found a GitHub issue exactly as I had. In the end, it’s really easy to make it work:

First we need to check if WP-CLI has been installed:

$ wp --version

This should print the WP-CLI version number. If not, you should install WP-CLI.

Next up, make sure you download wp-completion.bash and place it in your home directory.

After that open your .zshrc file with:

$ nano ~/.zshrc

Use ctrl+v to go to the bottom of this file and add the following code:

autoload -U +X bashcompinit && bashcompinit
source $HOME/wp-completion.bash

Exit and reopen your terminal window and cd to a WordPress directory. Type wp and press tab twice. You can select the command you want through the arrow keys and confirm with enter.

Follow @vadiemjanssens