How to enable command completion for Azure CLI in zsh?
Asked Answered
B

8

33

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

Bedaub answered 14/3, 2018 at 9:0 Comment(1)
Is auto-complete feature possible for window cmd or even powershell ?Tortuous
B
44

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.

Bedaub answered 14/3, 2018 at 12:1 Comment(2)
You should add both the autoload and source commands given above to your .zshrc file, autoload doesn't persist between sessions and must be run before the source command. Great answer, just want to clarify.Astrix
No sure why Microsoft is not putting some effort into the official support of ZSH.Benil
B
12

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.

Brandt answered 15/1, 2019 at 9:48 Comment(0)
T
11

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.

Terrel answered 3/12, 2021 at 14:13 Comment(1)
Path for Ubuntu: /home/linuxbrew/.linuxbrew/etc/bash_completion.d/azSprue
E
10

If your OS has /etc/bash_completion.d/azure-cli, then with oh-my-zsh it is as simple as:

$ ln -s /etc/bash_completion.d/azure-cli ~/.oh-my-zsh/custom/az.zsh
$ source ~/.zshrc

Alternatively you have to download it:

$ wget https://raw.githubusercontent.com/Azure/azure-cli/dev/az.completion \
  -O ~/.oh-my-zsh/custom/az.zsh
Entellus answered 9/11, 2021 at 20:5 Comment(1)
Even though the OP didn't mention oh-my-zsh, I assume most ZSH users use oh-my-zsh, which makes the answer more relevant. thank you!Eudiometer
R
2

In MacBook

  1. Download the Bash_completion script
  2. place the az bash completion script in /usr/local/etc/bash_completion.d
  3. Make sure az script with executable permissions .
  4. Update your .zshrc file as below autoload bashcompinit && bashcompinit source /usr/local/etc/bash_completion.d/az
  5. Restart your terminal.
Rudd answered 9/7, 2020 at 19:9 Comment(1)
Thanks for providing the paths for those of use who installed via Homebrew!Fluidics
W
0

For bash here are the steps:

1: AzureJumpBox $ cd /etc/bash_completion.d/ AzureJumpBox $ ls apport_completion azure-cli git-prompt grub

2: AzureJumpBox $ source /etc/bash_completion.d/azure-cli

3: AzureJumpBox $ az aks You will see all the options

Walcott answered 10/7, 2020 at 15:58 Comment(0)
P
0

I landed on this page searching for Zsh az completion tips. Based on the earlier posts, the following adds completion using Antidote for plugin management:

Add

Azure/azure-cli kind:clone path:az.completion

to your .zsh_plugins.txt file

In your .zshrc, before antidote load, add

autoload -Uz compinit
compinit
autoload -U +X bashcompinit
bashcompinit
Pigment answered 4/11, 2022 at 9:26 Comment(0)
R
0

Provided you installed az cli with brew*, this line added in ~/.zshrc does the trick:

[ -s "$(brew --prefix)/etc/bash_completion.d/az" ] && \. "$(brew --prefix)/etc/bash_completion.d/az"

* even if it's missing the terminal starts without errors due to the check at the beginning of the command.

Rinker answered 1/12, 2023 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.