A shell function for auto-completing script commands from package.json.
- Install jq and fzf (
sudo apt install jq fzf
) - Copy this one line function to your ~/.bashrc or ~/.zshrc (
source ~/.bashrc
orsource ~/.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.
Using this package.json as an example:
$ ppp t
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> ")";};
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.