/package-json-fzf

A fast package.json script command autocompletion with fzf

About

A shell function for auto-completing script commands from package.json.

Installation

  1. Install jq and fzf (sudo apt install jq fzf)
  2. Copy this one line function to your ~/.bashrc or ~/.zshrc (source ~/.bashrc or source ~/.zshrc to refresh in-place). You can also just copy-paste this into your terminal to play with it.
function ppp(){ pnpm $(jq -r ".scripts|keys[]"<$(npm root|sed 's/node_modules//')package.json|fzf -q "$1" );};

This uses pnpm - if you're using yarn or npm replace pnpm in the function with yarn or npm.

Usage

Using this package.json as an example:

$ ppp t

image

Other Uses

You can extend this for any pattern of "execute-command against result of fuzzy search against something".

For example, this shell function adds a command hi which you can run alone or type hi <first few letters>. It searches a dictionary and echo's "Your word is: "

function hi(){ echo "Your word is: $(cat /usr/share/dict/words | fzf -q "$1" --prompt "hi> ")";};

image

Your word is: worldliness

fzf also has an argment for executing a command against results: --bind "enter:execute(less {})", but this runs the command then brings you back into fzf which isn't ideal.