Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable command completion for Azure CLI in zsh?

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://learn.microsoft.com/en-us/cli/azure/get-started-with-azure-cli?view=azure-cli-latest#finding-commands

like image 230
josteinb Avatar asked Mar 14 '18 09:03

josteinb


People also ask

How do I use autocomplete in zsh?

zsh-autocomplete adds real-time type-ahead autocompletion to Zsh. Find as you type, then press Tab to insert the top completion, Shift Tab to insert the bottom one, or ↓ / PgDn to select another completion.

Can I use bash completion in zsh?

for ZSH usersZsh can handle bash completions functions. The latest development version of zsh has a function bashcompinit, that when run will allow zsh to read bash completion specifications and functions.

Where are zsh completions stored?

On macOS completions are stored in /usr/share/zsh/5.3/functions (replace the 5.3 with 5.7. 1 in Catalina). This directory stores many functions used with zsh and is in the default fpath . All the files in that directory that start with an underscore _ contain the completion definitions command.

How do you invoke the Azure CLI?

How to sign into the Azure CLI. Before using any Azure CLI commands with a local install, you need to sign in with az login. Run the login command. If the CLI can open your default browser, it will initiate authorization code flow and open the default browser to load an Azure sign-in page.


3 Answers

Also, the bash completion file should already be installed on your system.

Look for /etc/bash_completion.d/azure-cli

If the file is there, you can skip step 1 in accepted answer and source that file directly.

like image 90
teebszet Avatar answered Oct 19 '22 00:10

teebszet


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:

    autoload -U +X bashcompinit && bashcompinit
    
  3. 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.

like image 31
josteinb Avatar answered Oct 19 '22 00:10

josteinb


Installed Az CLI on macOS Monterey with Homebrew I've used this commands in my ~/.zshrc file:

autoload -U +X bashcompinit && bashcompinit
source /opt/homebrew/etc/bash_completion.d/az

Autocompletion was deployed to another location.

like image 5
Christoph Vollmann Avatar answered Oct 19 '22 02:10

Christoph Vollmann