Using fzf with kubectl as REPL
I was pretty impressed by the article fzf-live-repl and decide to adapt these techniques in my daily work with Kubernetes.
Using fzf as JQ repl
Command in the article above didn’t work for me:
echo '' | fzf --print-query --preview 'jq {q} /path/to/json/file'
After some investigation, it turned out that FZF_DEFAULT_OPTS, which I’ve
described in FZF on Fedora
were breaking preview. Also I wanted to be able to pass various resources and
arguments into the query. So to do this, I used following tricks:
- define function to be able to pass arguments
- use $* to pass all arguments into function
- set up local FZF environment option by using ()
I end up with following function:
kgjq() { (FZF_DEFAULT_OPTS='';kubectl get *$ -o json > /tmp/kgjq.json;echo '' | fzf --print-query --preview 'jq {q} /tmp/kgjq.json';) }
Here’s how it works:

As you might guess, after you press enter, resulting jsonpath will pop in
your command line. To make function work, put it into your .bash_profile
Using fzf as kubectl preview
Another cool feature I thought about was fuzzy finding kubernetes resources with live preview and ability to edit this resource without leaving fzf I know about k9s and kubectl-aliases but I tend to minimalism in my settings.
Again, I’ll be using bash function with $* to pass all variables into it:
kg() { kubectl get $* -o name | fzf --preview 'kubectl get {} -o yaml' --bind "enter:execute(kubectl get {} -o yaml | nvim )"; }
It’s working very well:

The trick I’m using here is binding enter in fzf to execute kubectl get,
passing selected resource name as parameter, and passing output to nvim(which can
read stdin without additional keys). I will think about adding --export option
to kubectl or passing multiple files to nvim, or about refreshing fuzzy list without
leaving fzf, but even now I’m really enjoying it.
Futher improvements
After a few days after writing this post I’ve done following improvementes to kg function:
kg() {
kubectl get $* -o name | \
fzf --preview 'kubectl get {} -o yaml' \
--bind "ctrl-\:execute(kubectl get {+} -o yaml | nvim )" \
--bind "ctrl-r:reload(kubectl get $* -o name)" --header 'Press CTRL-R to reload' \
--bind "ctrl-]:execute(kubectl edit {+})";
}
- Replace {} with {+} to allow editing multiple files
- Add new binding to edit
livekubernetes resource - Add new binding to reload preview list
So I end up using following shortcuts:
tab- multi-select entry under cursorctrl-a- select allctrl-r- reload preview listctrl-\- edit a local copy of selected resources in neovimctrl-]- editliveversion of kubernetes resource with default editorenter- exit from fzf and print current selectionshift-up- move preview window line upshift-down- move preview window line down

Comments