Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doggy8088/1b3f5ac1ccb5c6da14f732c6750274ae to your computer and use it in GitHub Desktop.
Save doggy8088/1b3f5ac1ccb5c6da14f732c6750274ae to your computer and use it in GitHub Desktop.

將 kubectl 改成 k 並同時擁有 bash completion 的設定方法

自動修改

kubectl completion bash | sudo tee /etc/bash_completion.d/k > /dev/null
sudo sed -e 's/local commands=("kubectl")/local commands=("k")/g' /etc/bash_completion.d/k -i
sudo sed -e 's/__start_kubectl kubectl/__start_kubectl k/g' /etc/bash_completion.d/k -i
echo 'alias k=kubectl' | tee -a ~/.profile > /dev/null
. ~/.profile

手動修改

  1. 先產生 bash completion 檔案

    kubectl completion bash | sudo tee /etc/bash_completion.d/k > /dev/null
  2. 編輯 /etc/bash_completion.d/k 檔案內容

    sudo vi /etc/bash_completion.d/k

    在檔案最末端,修改以下 3 個地方:

    • local commands=("kubectl") 改成 local commands=("k")
    • __start_kubectl kubectl 改成 __start_kubectl k (有兩處要改)

    image

  3. 設定 k 別名

    echo 'alias k=kubectl' | tee -a ~/.profile > /dev/null
    . ~/.profile
@zanhsieh
Copy link

zanhsieh commented Dec 14, 2018

# Mac OSX Run this script after upgrade kubectl from brew
VERSION=`kubectl version --short --client | awk -F'Client Version: v' '{print $2}'`
sed -i.bak 's/local commands=("kubectl")/local commands=("k")/g' /usr/local/Cellar/kubernetes-cli/${VERSION}/etc/bash_completion.d/kubectl
sed -i.bak 's/__start_kubectl kubectl/__start_kubectl k/g'       /usr/local/Cellar/kubernetes-cli/${VERSION}/etc/bash_completion.d/kubectl
rm -f /usr/local/Cellar/kubernetes-cli/${VERSION}/etc/bash_completion.d/kubectl.bak
mv /usr/local/Cellar/kubernetes-cli/${VERSION}/etc/bash_completion.d/{kubectl,k}
rm -f /usr/local/etc/bash_completion.d/kubectl
cd /usr/local/etc/bash_completion.d/
ln -s ../../Cellar/kubernetes-cli/${VERSION}/etc/bash_completion.d/k k
cd -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment