Giter VIP home page Giter VIP logo

jq-zsh-plugin's Introduction

jq zsh plugin

Interactively build jq expressions (gojq is also supported).

This zsh plugin gives you jq superpowers!

Fish users: you can use this friendly fork: rmartine-ias/jq-fish-plugin

Table of contents

Demos

Interactive jq query construction

asciicast

Insert jq query in the middle of a pipeline

asciicast

Installation

Besides jq, this plugin also requires fzf (a recent version) to be installed and available on your PATH.

The following installation methods are proven to work:

  1. Clone the repository:
git clone https://github.com/reegnz/jq-zsh-plugin.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/jq
  1. Add the plugin to the list of plugins for Oh My Zsh to load (inside ~/.zshrc):
plugins=(
	# other plugins...
	jq
)
  1. Start a new terminal session.
zplug reegnz/jq-zsh-plugin
antigen bundle reegnz/jq-zsh-plugin
zgen load reegnz/jq-zsh-plugin

Usage

  • type out a command that you expect to produce json on its standard output
  • press alt + j
  • start typing jq expression and watch it being evaluated in real time (like a true REPL!)
  • use up/down and hit tab to select one of the suggestions
  • or type out a jq query on your own
  • press enter, and the jq expression is appended to your initial command!

Key bindings

Bringing up the jq query builder for a shell command: alt + j

During interactive querying, the following shortcuts can be used:

Shortcut Effect
up Navigate path queries
down Navigate path queries
tab Select path query
shift + up Scroll up
shift + down Scroll down
alt + up Scroll up full page
alt + down Scroll down full page
ctrl+r Reload input

gojq support

If you want to use an alternative jq implementation, like gojq then you can override the default jq command used by the plugin. Set the following environment variable:

JQ_REPL_JQ=gojq

Internals

The project consists of the following components:

  • a jq.plugin.zsh providing a user-defined zsh line-editor widget, utilizing the jq-repl command
  • a jq-repl command to interactively build jq expressions, utilizing fzf for its UI
  • a jq-paths command to get all valid jq paths in the provided JSON document, used for suggesting paths.

Troubleshooting

MacOS: Pressing alt-j creates a symbol in iTerm2

You need to remap your alt-key to Esc+ in iTerm2:

  • Cmd + , to enter preferences
  • Go to Profiles
  • select your profile from the pane on the left hand side
  • go to the keys tab
  • Set Left Option (⌥ ) Key to Esc+

See other suggestions on stackoverflow if the above one doesn't help you: https://stackoverflow.com/q/196357/205318

Another option is to map to ctrl+j instead by putting this in your .zshrc:

bindkey `^j` jq-complete

Disable expanding shell aliases

The plugin automatically expands shell aliases in a command before passing it to jq-repl. To disable, put the following line into your .zshrc:

JQ_ZSH_PLUGIN_EXPAND_ALIASES=0

jq-zsh-plugin's People

Contributors

adriangoransson avatar ericcrosson avatar gerrit-k avatar jochenwierum avatar johnhamelink avatar plauko avatar reegnz avatar shirtpantswallet avatar xusiyuan841028 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

jq-zsh-plugin's Issues

Does not work with command aliases

First of all, this plugin is amazing!

However, it doesn't fully work with my workflow. I use it with kubectl and it does work properly with the full command, but, like many, i'm lazy and created the alias k for kubectl.
The problem is that when I use this syntax I get an empty REPL window.
Is there a way to configure the plugin, shell or alias to make aliases to work with this plugin?

Incompatible with oh-my-zsh

I tried using this as a plugin in oh-my-zsh, by cloning the repo into ~/.oh-my-zsh/custom/plugins, but oh-my-zsh doesn't recognize it as a plugin. It would be great to use this with oh-my-zsh

Suggest out put the query result, not the query-str

That is not sense to return the query-str, which is the default action by print-query.
I change it to print query result:

query=$(eval "$FZF_JQ_REPL_COMMAND" |
fzf
--preview "jq --color-output ${JQ_REPL_ARGS:-} {q} "$input""
--preview-window="down:90%"
--height="99%"
--query="."
--bind "tab:replace-query,return:print-query"
--bind "alt-up:preview-page-up,alt-down:preview-page-down"
--bind "ctrl-r:reload${fzf_notation}${FZF_JQ_REPL_COMMAND}${fzf_notation}+refresh-preview"
)
jq --color-output ${JQ_REPL_ARGS:-} $query "$input"

__get_query:5: command not found: jq-repl

I followed the instructions to add to oh-my-zsh, but got this error when doing alt+j.
__get_query:5: command not found: jq-repl

I fixed it by adding the bin dir to PATH
export PATH="$PATH:/Users/me/.oh-my-zsh/custom/plugins/jq/bin"

But am I supposed to do that manually? If so, could it be added to the installation instructions?

Will there be further optimization?

Hi, will this plugin be further optimized in the future, for example

  • Instant Results: See the output of your jq filter in real-time.
  • Error Handling: Encounter a syntax error? display the error message and the results of the closest valid filter.

discussions: jq and fzf
image

Using this plugin with iTerm2

Hi there,

I just wanted to leave a note to let you know how to use this plugin with Iterm2:

  1. Cmd + , to enter preferences
  2. Go to Profiles, select your profile from the pane on the left hand side, then go to the keys tab.
  3. Set Left Option (⌥) Key to Esc+

If you don't set things this way, alt+j will create a symbol.

That's all!

jq -r-f not found

As mentioned in #9 I have a bug while using the plugin that looks to be only esthetical.
At the top of the REPL window I sometime get an error message, and sometime I don't see the error message because it gets covered up by a line of 0/0 (#9 (comment))

zsh-jq-bug

I haven't noticed any issues with functionality.
My suspicion is that this has to do something with the fact that I load jq as a zsh plugin with this snippet:

zinit ice as"program" from"gh-r" bpick"*linux64*" mv"jq-* -> jq"
zinit load stedolan/jq

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.