Skip to content

Instantly share code, notes, and snippets.

@fathergoose
Created July 19, 2024 13:51
Show Gist options
  • Save fathergoose/27e6772530750a0097659c2902f6e483 to your computer and use it in GitHub Desktop.
Save fathergoose/27e6772530750a0097659c2902f6e483 to your computer and use it in GitHub Desktop.
Zsh / bash dynamic completions for npx binaries installed in a project
## Depends on gnu-find
# brew install findutils
## ~/.zshrc
# autoload bashcompinit
# bashcompinit
# source ~/.config/zsh/bashcompinit/npx.bash
_npx() {
local dir=$(pwd -P)
while [[ -n "$dir" ]]; do
if [[ ! -d $dir/node_modules/.bin ]]; then
dir=${dir%/*}
continue
fi
local execs=($(
cd $dir/node_modules/.bin
gfind -L . -type f -executable
))
execs=(${execs[@]/#.\//})
local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen -W "${execs[*]}" -- "$cur"))
break
done
}
complete -F _npx npx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment