Skip to main content
deleted 2 characters in body
Source Link
Stephen
  • 101
  • 2

I have noticed that a few responses are concerned with autocompleting with kubectl aliases. While other answers have addressed how to have auto complete on an alias for kubectl, they will not work when you have an alias for something like kubectl describe pod. I believe this is related to how kubectl does completion and not about completion in general.

Alias completion for kubectl version 1.27.1

Putting the following in my .bash_profile works to get multiword kubectl aliases working for v1.27.1:

# Load kubectl completion functions
# Need to run:
#    kubectl completion bash > ~/.kubectl-completion
# To generate the .kubectl-completion file
source ~/.kubectl-completion

# Create the pod-show alias
alias pod-show="kubectl describe pod"

# Modified from the accepted answer
__start_kubectl_pods() {
  local compl
  compl=${COMP_WORDS[@]:1}
  (( COMP_CWORD += 2 ))
  COMP_WORDS=( kubectl describe pod "${compl:=- }" )
  __start_kubectl
}

# Setup completion
complete -o default -F __start_kubectl_pods pod-show

A couple of notes:

  • the __start_kubectl is the main function for completing kubectl commmands. This function requires a space at the end to do completion on no arguments. I believe this is why the accepted answer does not work out of the box.
  • Running export BASH_COMP_DEBUG_FILE=compdebug and then trying the completion outputs debug logs to a file named compdebug. This helped with figuring this out.

I have noticed that a few responses are concerned with autocompleting with kubectl aliases. While other answers have addressed how to have auto complete on an alias for kubectl, they will not work when you have an alias for something like kubectl describe pod. I believe this is related to how kubectl does completion and not about completion in general.

Alias completion for kubectl version 1.27.1

Putting the following in my .bash_profile works to get multiword kubectl aliases working for v1.27.1:

# Load kubectl completion functions
# Need to run:
#    kubectl completion bash > ~/.kubectl-completion
# To generate the .kubectl-completion file
source ~/.kubectl-completion

# Create the pod-show alias
alias pod-show="kubectl describe pod"

# Modified from the accepted answer
__start_kubectl_pods() {
  local compl
  compl=${COMP_WORDS[@]:1}
  (( COMP_CWORD += 2 ))
  COMP_WORDS=( kubectl describe pod "${compl:= }" )
  __start_kubectl
}

# Setup completion
complete -o default -F __start_kubectl_pods pod-show

A couple of notes:

  • the __start_kubectl is the main function for completing kubectl commmands. This function requires a space at the end to do completion on no arguments. I believe this is why the accepted answer does not work out of the box.
  • Running export BASH_COMP_DEBUG_FILE=compdebug and then trying the completion outputs debug logs to a file named compdebug. This helped with figuring this out.

I have noticed that a few responses are concerned with autocompleting with kubectl aliases. While other answers have addressed how to have auto complete on an alias for kubectl, they will not work when you have an alias for something like kubectl describe pod. I believe this is related to how kubectl does completion and not about completion in general.

Alias completion for kubectl version 1.27.1

Putting the following in my .bash_profile works to get multiword kubectl aliases working for v1.27.1:

# Load kubectl completion functions
# Need to run:
#    kubectl completion bash > ~/.kubectl-completion
# To generate the .kubectl-completion file
source ~/.kubectl-completion

# Create the pod-show alias
alias pod-show="kubectl describe pod"

# Modified from the accepted answer
__start_kubectl_pods() {
  local compl
  compl=${COMP_WORDS[@]:1}
  (( COMP_CWORD += 2 ))
  COMP_WORDS=( kubectl describe pod "${compl:- }" )
  __start_kubectl
}

# Setup completion
complete -o default -F __start_kubectl_pods pod-show

A couple of notes:

  • the __start_kubectl is the main function for completing kubectl commmands. This function requires a space at the end to do completion on no arguments. I believe this is why the accepted answer does not work out of the box.
  • Running export BASH_COMP_DEBUG_FILE=compdebug and then trying the completion outputs debug logs to a file named compdebug. This helped with figuring this out.
Source Link
Stephen
  • 101
  • 2

I have noticed that a few responses are concerned with autocompleting with kubectl aliases. While other answers have addressed how to have auto complete on an alias for kubectl, they will not work when you have an alias for something like kubectl describe pod. I believe this is related to how kubectl does completion and not about completion in general.

Alias completion for kubectl version 1.27.1

Putting the following in my .bash_profile works to get multiword kubectl aliases working for v1.27.1:

# Load kubectl completion functions
# Need to run:
#    kubectl completion bash > ~/.kubectl-completion
# To generate the .kubectl-completion file
source ~/.kubectl-completion

# Create the pod-show alias
alias pod-show="kubectl describe pod"

# Modified from the accepted answer
__start_kubectl_pods() {
  local compl
  compl=${COMP_WORDS[@]:1}
  (( COMP_CWORD += 2 ))
  COMP_WORDS=( kubectl describe pod "${compl:= }" )
  __start_kubectl
}

# Setup completion
complete -o default -F __start_kubectl_pods pod-show

A couple of notes:

  • the __start_kubectl is the main function for completing kubectl commmands. This function requires a space at the end to do completion on no arguments. I believe this is why the accepted answer does not work out of the box.
  • Running export BASH_COMP_DEBUG_FILE=compdebug and then trying the completion outputs debug logs to a file named compdebug. This helped with figuring this out.