DAGDA SEARCH | Back to results | Support

Implementing a jq REPL with fzf · GitHub

Source: https://gist.github.com/reegnz/b9e40993d410b75c2d866441add2cb55

Instantly share code, notes, and snippets.

@reegnz

reegnz/README.md

Last active November 12, 2025 20:57 Show Gist options

Select an option

No results found

Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/reegnz/b9e40993d410b75c2d866441add2cb55.js"></script> Save reegnz/b9e40993d410b75c2d866441add2cb55 to your computer and use it in GitHub Desktop. Download ZIP Implementing a jq REPL with fzf Raw README.md

Implementing a jq REPL with fzf

Update: I created jq-zsh-plugin that does this.

One of my favourite tools of my trade is jq. It essentially enables you to process json streams with the same power that sed, awk and grep provide you with for editing line-based formats (csv, tsv, etc.).

Another one of my favourite tools is fzf. Essentially it is just a command line fuzzy finder, that has some awesome interactive uses.

Now in this REPL case it's fuzzyness isn't particularly necessary, but lucky for me it also has an awesome preview functionality as well.

I took this idea from another blog post about turning simple commands into a live REPL

The full code is the following:

#!/usr/bin/env bash
if [[ -z $1 ]] || [[ $1 == "-" ]]; then
    input=$(mktemp)
    trap "rm -f $input" EXIT
    cat /dev/stdin > $input
else
    input=$1
fi

echo '' \
    | fzf --phony \
          --preview-window='up:90%' \
          --print-query \
          --preview "jq --color-output -r {q} $input"

Let's walk through what the script does.

Input handling

if [[ -z $1 ]] || [[ $1 == "-" ]]; then
    input=$(mktemp)
    trap "rm -f $input" EXIT
    cat /dev/stdin > $input
else
    input=$1
fi

The first part is just input handling. If there is an argument, it assumes it's a json file to be fed to fzf.

If there is no argument, or the first argument is -, then it assumes it needs to cache the input coming from stdout. It caches the input into a temporary file which it will delete on exit with a trap statement.

FZF preview

fzf has a --preview mode that runs a pre-defined command when you change your query input. The --print-query option will print out the query you wrote as the first line once fzf quits.

echo '' | fzf --print-query --preview 'echo {q}'

Feeding fzf with an empty input will mean no fuzzy search can be done, but whatever input you feed it can be used in the preview window and it updates the preview whenever the query changes. This is where we will hoist our REPL onto.

Core REPL code

echo '' | fzf --print-query --preview 'jq {q} input.json'

That's it, our REPL is done! I find this small form fascinating. Now what is still missing, that landed in the final script:

  1. parameterize the jq input with any file
  2. capture standard input, buffer it and use that as the jq input
  3. formating the preview window

All of the above is implemented in the first example code.

After I've writen this post I've also found this nice asciicast that shows a generalized fzf REPL idea in action.

Raw jq-repl This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters
#!/usr/bin/env bash
if [[ -z $1 ]] || [[ $1 == "-" ]]; then
input=$(mktemp)
trap "rm -f $input" EXIT
cat /dev/stdin > $input
else
input=$1
fi
echo '' \
| fzf --phony \
--preview-window='up:90%' \
--print-query \
--preview "jq --color-output -r {q} $input"
@reegnz

reegnz commented Jan 23, 2020

Copy link Copy Markdown Author

Demo: https://asciinema.org/a/ZesX97hokd5VLuEXaeyjJV2Yy

@pvonmoradi

pvonmoradi commented Jun 26, 2021

Copy link Copy Markdown

Interesting script! Here are some suggestions:

Here is the patched script:

#!/usr/bin/env bash
if [[ -z $1 ]] || [[ $1 == "-" ]]; then
    input=$(mktemp)
    trap 'rm -f "$input"' EXIT
    cat /dev/stdin > "$input"
else
    input=$1
fi

echo '' \
    | fzf --disabled \
        --preview-window='up:90%' \
        --print-query \
        --preview "jq --color-output -r {q} $input"
@reegnz

reegnz commented Jun 29, 2021

Copy link Copy Markdown Author

Thanks for that feedback!
Continued improvements to this script live here: https://github.com/reegnz/jq-zsh-plugin
I will try to get the improvements in there, but PR-s are also welcome!

@davidkuda

davidkuda commented Nov 3, 2022

Copy link Copy Markdown

This is ingenious! Thanks so much for sharing it, a lot of fun to use. :)

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