Skip to content

Instantly share code, notes, and snippets.

@ozbillwang
Last active March 28, 2024 18:46
Show Gist options
  • Save ozbillwang/ea1f8df755f63f5812b06bb708017b36 to your computer and use it in GitHub Desktop.
Save ozbillwang/ea1f8df755f63f5812b06bb708017b36 to your computer and use it in GitHub Desktop.
Kubernetes auto-completion works with FISH (better than Bash)

Why use fish for kubernetes autocompletion

I am stucking on kubernetes autocompletion on my macbook for a while. Whatever I try with the document (https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion) it does not work

Enable kubernetes automation in fish is the simplest solution

# install fish
$ brew install fish


# set kubectl completion for fish shell
$ mkdir -p ~/.config/fish/completions
$ cd ~/.config/fish
$ git clone https://github.com/evanlucas/fish-kubectl-completions
$ ln -s ../fish-kubectl-completions/completions/kubectl.fish completions/

# if you have kubernetes cluster, skip this part
$ brew install kink
$ kind create cluster

# Now you can get autocompletion directly

$ fish
Welcome to fish, the friendly interactive shell
Type `help` for instructions on how to use fish
bill@192-168-1-107 ~/.kube> kubectl get node
NAME                 STATUS   ROLES    AGE     VERSION
kind-control-plane   Ready    master   4m31s   v1.17.0
kind-worker          Ready    <none>   3m57s   v1.17.0
kind-worker2         Ready    <none>   3m57s   v1.17.0
kind-worker3         Ready    <none>   3m57s   v1.17.0
bill@192-168-1-107 ~/.kube> kubectl describe pod/jenkins-1591700084-6b4ff48648-h57pq

enjoy it. It has more features than you are thinking.

work with Z jump around

go through this repo: https://github.com/sjl/z-fish

can't use $ any more

So you have problem with $ in command, such as

export files=$(ls)

You have to replace with

export files=(ls)

No Ctrl+R?

Powerful History Mechanism

Modern shells save previous commands in a command history. You can view earlier commands by using the up and down arrows. Fish extends this concept by integrating the history search functionality. To search the history, simply type in the search string, and press the up arrow. By using the up and down arrow, you can search for older and newer matches. The fish history automatically removes duplicate matches and the matching substring is highlighted. These features make searching and reusing previous commands much faster.

"while read" goes wired

I can't run below script easily in fish now,

ls |while read line
do
  echo $line
done

You have to adjust to

ls |while read line
  echo $line
end

reference

https://lwn.net/Articles/136232/

https://github.com/jorgebucaran/fish-cookbook#how-do-i-read-from-a-file-in-fish

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