diwakersurya
6/21/2016 - 7:07 AM

npm install autocompletion for bash. Provides content of `~/.npm` as autocomplete options for `npm install` :sparkles: https://medium.com/@j

npm install autocompletion for bash. Provides content of ~/.npm as autocomplete options for npm install :sparkles: https://medium.com/@jamischarles/adding-autocomplete-to-npm-install-5efd3c424067#.8w0jzkh3w

#!/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