kubectl: Cannot autocomplete kubectl bash aliases

The default kubectl complete function __start_kubectl does not work with bash aliases:

# ~/.aliases
alias k="kubectl"
complete -o default -F __start_kubectl k

Actual output:

~$ k [TAB]
k kubectl

~$ k k [TAB]
k kubectl

Expected output:

~$ k [TAB]
annotate        convert         expose          rolling-update
api-versions    cordon          get             rollout
apply           cp              label           run
attach          create          logs            scale
auth            delete          options         set
autoscale       describe        patch           taint
certificate     drain           plugin          top
cluster-info    edit            port-forward    uncordon
completion      exec            proxy           version
config          explain         replace

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 36
  • Comments: 40 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I don’t know if this is the best/right solution, but I made k autocomplete by doing source <(kubectl completion bash | sed 's/kubectl/k/g').

https://github.com/cykerway/complete-alias is kind of amazing. With this, there is no need to modify the original completion functions, and it works with lazily-loaded completions.

The official instructions are not working for me with bash 4.3 and 4.4. After adding the commands below, completing with kubectl works fine, but k doesn’t. If I type ‘k’, ‘space’ and then press tab is expands to ‘k kubectl’ and then if I press tab again it starts completing kubectl commands. Do the official approach only work with bash 5.x?

This does not work, k completes to k kubectl:

source <(kubectl completion bash)
alias k=kubectl
complete -F __start_kubectl k

Using complete_alias does work:

source <(kubectl completion bash)
alias k=kubectl
complete -F _complete_alias k

And this workaround of duplicating the completions does also work:

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

I followed official guide and the issue is gone.

https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion

What I was missing is

echo 'complete -F __start_kubectl k' >>~/.bashrc

/unsubscribe

/assign

I don’t know if this is the best/right solution, but I made k autocomplete by doing source <(kubectl completion bash | sed 's/kubectl/k/g').

I added this into ~/.bash_profile

@weibeld Yes. Where _complete_alias is __start_kubectl

Can someone help to update the vendor to pick up the fix in cobra?