Last active
July 13, 2016 03:58
-
-
Save jamischarles/752ad319df780122c772168ad0bbc67e to your computer and use it in GitHub Desktop.
npm install autocompletion for bash. Provides content of `~/.npm` as autocomplete options for `npm install` ✨ https://medium.com/@jamischarles/adding-autocomplete-to-npm-install-5efd3c424067#.8w0jzkh3w
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash #adding this to force silly gist highlighting. REMOVE THIS | |
# 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