What is Zsh?

Zsh, also known as Z shell, is a command line shell designed as an alternative to the traditional Bourne shell (sh) and Bourne Again Shell (bash). It is an extended version of the standard Unix shell with additional features, improved usability, and enhanced customization options.

Some advantages of Zsh over other shells include:

  • Tab completion
  • Enhanced globbing
  • Plugin and theme support
  • Improved scripting capabilities
  • Interactive use enhancements
  • Powerful configuration options

Zsh allows extensive customization through its configuration file, usually located at ~/.zshrc. You can define aliases, environment variables, shell options, and more to tailor Zsh to your specific needs.

Add Zsh Autocompletion

Zsh supports kubectl autocompletion but it is not configured by default. Autocompletion in your CLI will allow you to interact faster and to receive command prompts with use of the Tab button. There are several ways to install this feature for kubectl and we will go through one method now. This method should work for MacOs with Zsh version > 5.2.

Use a text editor to open your existing ~/.zshrc file:

vim ~/.zshrc
None

Add the following to your ~/.zshrc file:

#kubectl autocompletion
autoload -Uz compinit
compinit
source <(kubectl completion zsh)
None

Save and close the file (if using Vim, do :wq ). To load this new function into the current terminal, use the source command:

source ~/.zshrc 
None

Now try to use autocomplete. You can type the start of your command then hit the Tab button. For example, I am running a Kubernetes cluster with some deployments. If I type kubectl get d and hit the Tab button, I will get these options:

None

If I then type an a and hit Tab, the CLI will autocomplete daemonsets.apps .

None

If the above steps do not result in autocompletion working, continue on to the next section.

Troubleshooting

If you get an error like this:

None

Close ALL of your terminal windows. Open a new window and try autocompletion again. If it still does not work, run source ~/.zshrc again. Now try autocompletion again. Sometimes, when you add to your ~/.zshrc file and load your new function with the source command, it wants a brand new terminal to do this. If you get other errors, you'll have to troubleshoot them, as this was the only one I ran into. Hope this helped you install Zsh autocompletion and thanks for reading!

None