Kubernetes kubectl bash completion with alias

10/20/2018

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

-- Ijaz Ahmad Khan
kubernetes

4 Answers

1/13/2020

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 comopletion)

echo 'complete -F __start_kubectl k' >>~/.bashrc (make them work together)

So, you execute commands like 'k get logs -n my-namespace -f my-p[TAB]' (to complete name of pod for example).

-- WyllianNeo
Source: StackOverflow

5/20/2019

from the official docs

# after installing bash completion

alias k=kubectl
complete -F __start_kubectl k

https://kubernetes.io/docs/reference/kubectl/cheatsheet/#bash

-- endline
Source: StackOverflow

10/20/2018

You can basically do this:

$ echo "source <(kubectl completion bash | sed 's|__start_kubectl kubectl|__start_kubectl ks|g') >> ~/.bashrc

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
-- Rico
Source: StackOverflow

10/21/2018

I have this in my .bashrc to get alias and auto completion.

source <(kubectl completion bash | sed s/kubectl/k/g)

-- Praveen Sripati
Source: StackOverflow