Enable Tanzu CLI Auto-Completion in bash and zsh

Technology keeps moving but this post has not.

What you're about to read hasn't been updated in more than a year. The information may be out of date. Let me know if you see anything that needs fixing.

Lately I've been spending some time getting more familiar with VMware's Tanzu Community Edition Kubernetes distribution, but I'm still not quite familiar enough with the tanzu command line. If only there were a better way for me to discover the available commands for a given context and help me type them correctly...

Oh, but there is! You see, one of the available Tanzu commands is tanzu completion [shell], which will spit out the necessary code to generate handy context-based auto-completions appropriate for the shell of your choosing (provided that you choose either bash or zsh, that is).

Running tanzu completion --help will tell you what's needed, and you can just copy/paste the commands appropriate for your shell:

 1# Bash instructions:
 3  ## Load only for current session:
 4  source <(tanzu completion bash)
 6  ## Load for all new sessions:
 7  tanzu completion bash >  $HOME/.tanzu/completion.bash.inc
 8  printf "\n# Tanzu shell completion\nsource '$HOME/.tanzu/completion.bash.inc'\n" >> $HOME/.bash_profile
10# Zsh instructions:
12  ## Load only for current session:
13  source <(tanzu completion zsh)
15  ## Load for all new sessions:
16  echo "autoload -U compinit; compinit" >> ~/.zshrc
17  tanzu completion zsh > "${fpath[1]}/_tanzu"

So to get the completions to load automatically whenever you start a bash shell, run:

1tanzu completion bash >  $HOME/.tanzu/completion.bash.inc
2printf "\n# Tanzu shell completion\nsource '$HOME/.tanzu/completion.bash.inc'\n" >> $HOME/.bash_profile

For a zsh shell, it's:

1echo "autoload -U compinit; compinit" >> ~/.zshrc
2tanzu completion zsh > "${fpath[1]}/_tanzu"

And that's it! The next time you open a shell (or source your relevant profile), you'll be able to [TAB] your way through the Tanzu CLI!

Tanzu CLI completion in zsh