K
Q

Kubernetes kubectl bash completion with alias

October 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
kubernetes
bash-completion

6 Answers

May 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

October 20, 2018

A more recent update from the docs:

  • Source the completion script in your ~/.bashrc file:
    echo 'source <(kubectl completion bash)' >>~/.bashrc
  • Add the completion script to the /etc/bash_completion.d directory:
    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
-- Rico
Source: StackOverflow

October 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

January 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 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.

-- WyllianNeo
Source: StackOverflow

June 15, 2022
  1. first run below command

    sudo kubectl completion bash > /etc/bash_completion.d/kubectl
  2. 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
  3. restart your shell

-- mati kepa
Source: StackOverflow

February 23, 2022

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:

https://github.com/bhakta0007/kube-alias

-- Bhakta Raghavan
Source: StackOverflow