Skip to content

Instantly share code, notes, and snippets.

@dantailby
Created June 17, 2016 21:16
Show Gist options
  • Save dantailby/47075fc17bef981613a632375383ba1b to your computer and use it in GitHub Desktop.
Save dantailby/47075fc17bef981613a632375383ba1b to your computer and use it in GitHub Desktop.
bash npm autocompletion
# BASH standalone npm install autocomplete. Add this to ~/.bashrc file.
_npm_install_completion () {
local words cword
if type _get_comp_words_by_ref &>/dev/null; then
_get_comp_words_by_ref -n = -n @ -w words -i cword
else
cword="$COMP_CWORD"
words=("${COMP_WORDS[@]}")
fi
local si="$IFS"
# if your npm command includes `install` or `i `
if [[ ${words[@]} =~ 'install' ]] || [[ ${words[@]} =~ 'i ' ]]; then
local cur=${COMP_WORDS[COMP_CWORD]}
# supply autocomplete words from `~/.npm`, with $cur being value of current expansion like 'expre'
COMPREPLY=( $( compgen -W "$(ls ~/.npm )" -- $cur ) )
fi
IFS="$si"
}
# bind the above function to `npm` autocompletion
complete -o default -F _npm_install_completion npm
## END BASH npm install autocomplete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment