Skip to content

Instantly share code, notes, and snippets.

@peterjaffray
Forked from gibatronic/README.md
Created December 6, 2020 17:57
Show Gist options
  • Save peterjaffray/9bebbfac0d0d8b659d0b5a4ffb93a058 to your computer and use it in GitHub Desktop.
Save peterjaffray/9bebbfac0d0d8b659d0b5a4ffb93a058 to your computer and use it in GitHub Desktop.
Tab completion for the npx command

complete_npx

Bash tab completion for the npx command

Usage

Save complete_npx in your home folder and then source it on your .bash_profile with:

. ~/complete_npx.sh
no_modules() {
local project_root=$1
[ ! -r "$project_root/node_modules" ]
}
complete_npx() {
local project_root=$(pwd -P)
while no_modules "$project_root"; do
if [ "$project_root" = '/' ]; then
return 1
fi
project_root=$(dirname "$project_root")
done
local words=$(find "$project_root/node_modules/.bin" | tail -n +2 | xargs -L 1 -- basename)
local current_word=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "${words[@]}" -- "$current_word" ) )
}
complete -F complete_npx npx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment