Skip to content

Instantly share code, notes, and snippets.

@jmarucha
Forked from gibatronic/README.md
Last active September 23, 2018 19:56
Show Gist options
  • Save jmarucha/288d0f299d11801c1dda23b196046471 to your computer and use it in GitHub Desktop.
Save jmarucha/288d0f299d11801c1dda23b196046471 to your computer and use it in GitHub Desktop.
Tab completion for the npx command

complete_npx

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
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 CURR_ARG
local FILES=$(find "$project_root/node_modules/.bin" | tail -n +2 | xargs basename -a)
CURR_ARG=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "${FILES[@]}" -- $CURR_ARG ) );
}
complete -F complete_npx npx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment