Overview of Shell Autocompletions
I simply love shell completion on the command line, which saves you from a lot of spelling errors and therefore saves you a lot of time in your daily working. With Autocompletion you can press <TAB> when you're typing a command, and the shell will show you what the options are.
But Shells like my beloved bash or the Z Shell (zsh) don't provide this feature by themself, because they don't know which options are available. For this a lot of scripts exist, that teach the different options to your Shell. In this article I will list the Autocompletion scripts I use in my envrionments and show you how to install them.
Prerequisites
Brew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> ~/.bash_profile
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
sudo apt-get install -y build-essential
brew install gccBash Autocompletion
First install the package bash-completion using Homebrew:
brew install bash-completionecho '[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"' >> ~/.bash_profileZ Shell Autocompletion
First install the package bash-completion using Homebrew:
brew install zsh-completionsAfter finishing this installation, you'll get some instructions similiar to the following, which you'll need to follow. After this the setup for zsh is completed.
if type brew &>/dev/null; then
FPATH=$(brew --prefix)/share/zsh-completions:$FPATH
autoload -Uz compinit
compinit
fi
You may also need to force rebuild `zcompdump`:
rm -f ~/.zcompdump; compinit
Additionally, if you receive "zsh compinit: insecure directories" warnings when attempting
to load these completions, you may need to run this:
chmod go-w '/usr/local/share'Kubectl
Bash
If you want to enable kubectl Autocompletion for your current user only use:
echo 'source <(kubectl completion bash)' >> ~/.bashrcIf you want to enable the Autocompletion system-wide for every user use:
kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/nullIf you have an alias for kubectl like k, you can extend shell completion to work with that alias as well:
echo 'alias k=kubectl' >> ~/.bashrc
echo 'complete -F __start_kubectl k' >> ~/.bashrcZ Shell (zsh)
You can enable the kubectl Autocompletion for zsh for your user using:
echo 'source <(kubectl completion zsh)' >> ~/.zshrc
OpenShift CLI
Bash
If you want to enable oc Autocompletion for your current user only use:
echo 'source <(oc completion bash)' >> ~/.bashrcIf you want to enable the Autocompletion system-wide for every user use:
oc completion bash | sudo tee /etc/bash_completion.d/oc > /dev/nullZ Shell (zsh)
You can enable the kubectl Autocompletion for zsh for your user using:
echo 'source <(oc completion zsh)' >> ~/.zshrc
Krew
Krew is the plugin manager for kubectl and opens up a world of dozens of plugins that make working with kubectl and Kubernetes more comfortable. If you haven't installed Krew yet, check out my tutorial.
Bash
If you want to enable krew Autocompletion for your current user only use:
echo 'source <(kubectl krew completion bash)' >> ~/.bashrcIf you want to enable the Autocompletion system-wide for every user use:
kubectl krew completion bash | sudo tee /etc/bash_completion.d/krew > /dev/nullZ Shell (zsh)
You can enable the krew Autocompletion for zsh for your user using:
echo 'source <(kubectl krew completion zsh)' >> ~/.zshrc
Kubectx and Kubens
Kubectx and kubens are probably the most useful krew plugins you can use in your Kubernetes environment.
Bash
git clone https://github.com/ahmetb/kubectx.git ~/.kubectx
sudo mv ~/.kubectx/completion/kubectx.bash /etc/bash_completion.d/kubectx
sudo mv ~/.kubectx/completion/kubens.bash /etc/bash_completion.d/kubens
rm -rf ~/.kubectxZ Shell (zsh)
mkdir -p ~/.oh-my-zsh/completions
chmod -R 755 ~/.oh-my-zsh/completions
ln -s /opt/kubectx/completion/_kubectx.zsh ~/.oh-my-zsh/completions/_kubectx.zsh
ln -s /opt/kubectx/completion/_kubens.zsh ~/.oh-my-zsh/completions/_kubens.zshHelm
Bash
To load completions in your current shell session:
source <(helm completion bash)To load completions for every new session, execute as root user:
helm completion bash > sudo tee /etc/bash_completion.d/helm > /dev/nullZ Shell (zsh)
To load completions in your current shell session:
source <(helm completion zsh)To load completions for every new session, execute as root user:
helm completion zsh > "${fpath[1]}/_helm"Operator SDK
Bash
To load completions in your current shell session:
source <(operator-sdk completion bash)To load completions for every new session, execute as root user:
operator-sdk completion bash > sudo tee /etc/bash_completion.d/operator-sdk > /dev/nullZ Shell (zsh)
source <(operator-sdk completion zsh)To load completions for every new session, execute as root user:
operator-sdk completion zsh > "${fpath[1]}/_operator-sdk"Velero
Velero is the most-versatile solutions for backup and disaster recovery management in a Kubernetes environment. With these commands you can easily set-up autocompletion.
Bash
For your current user simply type:
echo 'source <(velero completion bash)' >>~/.bashrcIf you want to enable it system-wide use:
sudo velero completion bash >/etc/bash_completion.d/veleroWhen you are using a shortcut for velero you need to activate the autocompletion for this shortcut as well:
echo 'alias v=velero' >>~/.bashrc
echo 'complete -F __start_velero v' >>~/.bashrcZ Shell (zsh)
Instead of using the .bashrc you need to use .zshrc for the Z Shell:
echo 'source <(velero completion zsh)' >> ~/.zshrcecho 'alias v=velero' >>~/.zshrc
echo 'complete -F __start_velero v' >>~/.zshrcTerraform
Bash and Z Shell (zsh)
Terraform makes it very easy to use Autocompletion as it comes with a simple cli command:
terraform -install-autocompleteAfter installation you need to restart your shell.