I am using
kubectl with bash completion, but I prefer to use a shorter alias for kubectl such as ks, what changes I need to make to get the bash completion work with alias ksfrom the official docs
# after installing bash completion
alias k=kubectl
complete -F __start_kubectl k
https://kubernetes.io/docs/reference/kubectl/cheatsheet/#bash
A more recent update from the docs:
echo 'source <(kubectl completion bash)' >>~/.bashrc
kubectl completion bash >/etc/bash_completion.d/kubectl
If you have an alias for kubectl, you can extend shell completion to work with that alias:
echo 'alias ks=kubectl' >>~/.bashrc
echo 'complete -F __start_kubectl ks' >>~/.bashrc
You can basically do this:
$ echo "source <(kubectl completion bash | sed 's|__start_kubectl kubectl|__start_kubectl ks|g') >> ~/.bashrc
Out of date:
A slight change from what is described here.
In essence, you are substituting the following in the
kubectl completion bash output:
if [[ $(type -t compopt) = "builtin" ]]; then
complete -o default -F __start_kubectl kubectl
else
complete -o default -o nospace -F __start_kubectl kubectl
fi
With this:
if [[ $(type -t compopt) = "builtin" ]]; then
complete -o default -F __start_kubectl ks
else
complete -o default -o nospace -F __start_kubectl ks
fi
I have this in my .bashrc to get alias and auto completion.
source <(kubectl completion bash | sed s/kubectl/k/g)
Just to complement the awnser of endline (his solution works just works in active session of shell, if you close, you have to reexecute) and maybe help someone with the same trouble I was getting.
You can add it to the shell permanently using the structure above from the documentation
echo 'alias k=kubectl' >>~/.bashrc (add alias to shell)
echo 'source <(kubectl completion bash)' >>~/.bashrc (add completion)
echo 'complete -F __start_kubectl k' >>~/.bashrc (make them work together)
So, now you can execute commands like 'k get logs -n my-namespace -f my-p[TAB]' (to complete name of pod for example) even if close and open a new session.
first run below command
sudo kubectl completion bash > /etc/bash_completion.d/kubectl
and add this at the end of .bashrc
source /usr/share/bash-completion/bash_completion
source /etc/bash_completion
source <(kubectl completion bash)
alias k=kubectl
complete -F __start_kubectl k
restart your shell
I found that existing kubernetes aliases still require typing many arguments, especially when working across namespaces.
I wrote this tiny utility that has been working well for me in terms of reducing kubectl 'typing stress'. It does not cover all the commands but it is easy to extend: