Overview of Shell Autocompletions

Overview of Shell Autocompletions
Photo by Gabriel Heinzer / Unsplash

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 gcc

Bash Autocompletion

First install the package bash-completion using Homebrew:

brew install bash-completion
echo '[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"' >> ~/.bash_profile

Z Shell Autocompletion

First install the package bash-completion using Homebrew:

brew install zsh-completions

After 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)' >> ~/.bashrc

If you want to enable the Autocompletion system-wide for every user use:

kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null

If 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' >> ~/.bashrc

Z 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)' >> ~/.bashrc

If you want to enable the Autocompletion system-wide for every user use:

oc completion bash | sudo tee /etc/bash_completion.d/oc > /dev/null

Z 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)' >> ~/.bashrc

If you want to enable the Autocompletion system-wide for every user use:

kubectl krew completion bash | sudo tee /etc/bash_completion.d/krew > /dev/null

Z 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 ~/.kubectx

Z 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.zsh

Helm

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/null

Z 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/null

Z 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)' >>~/.bashrc

If you want to enable it system-wide use:

sudo velero completion bash >/etc/bash_completion.d/velero

When 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' >>~/.bashrc

Z Shell (zsh)

Instead of using the .bashrc you need to use .zshrc for the Z Shell:

echo 'source <(velero completion zsh)' >> ~/.zshrc
echo 'alias v=velero' >>~/.zshrc
echo 'complete -F __start_velero v' >>~/.zshrc

Terraform

Bash and Z Shell (zsh)

Terraform makes it very easy to use Autocompletion as it comes with a simple cli command:

terraform -install-autocomplete

After installation you need to restart your shell.