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 ks
Type 'echo 'source <(kubectl completion bash)' >>~/. bashrc' command in your command line tool, and you are done.
Bash completion is a bash function that allows you to auto complete commands or arguments by typing partially commands or arguments, then pressing the [Tab] key. This will help you when writing the bash command in terminal.
If the autocomplete results contain directories only (no files), then Bash Completion is installed. If the autocomplete results include files, then Bash Completion is not installed.
from 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 k' >>~/.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With