How to enable command completion for Azure CLI in zsh?

josteinb picture josteinb · Mar 14, 2018 · Viewed 8.9k times · Source

I've found hints at there being command completion available for bash[1] for the Azure CLI (az command), but I have not found any indication on how to install/enable that for zsh. Anyone know how to do that, if it is possible? I use oh-my-zsh, if that is relevant.

[1] https://docs.microsoft.com/en-us/cli/azure/get-started-with-azure-cli?view=azure-cli-latest#finding-commands

Answer

josteinb picture josteinb · Mar 14, 2018

It is possible to have completions for az in zsh.

  1. Get the completions for bash from the Azure CLI git repo and store this file somewhere your zsh startup script can find it: https://raw.githubusercontent.com/Azure/azure-cli/dev/az.completion

  2. Enable bash autocompletions in zsh (if it's not enabled already) [1]:

autoload -U +X bashcompinit && bashcompinit
  1. Enable the command completions for az: source /path/to/az.completion

The code snippets from step 2 and 3 can be added to a shell startup file (.zshrc or similar) to make the changes permanent.

[1] Can a Bash tab-completion script be used in zsh?