Skip to content

Instantly share code, notes, and snippets.

@jamischarles
Last active May 27, 2016 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamischarles/10728b504c69ba9760f70b38bfe2dd27 to your computer and use it in GitHub Desktop.
Save jamischarles/10728b504c69ba9760f70b38bfe2dd27 to your computer and use it in GitHub Desktop.
Make these changes to augment your local npm autocompletion to include autocomplete for `npm install`
#!/usr/bin/env bash #adding this to force silly gist highlighting
# If you are already using npm autocompletion via (https://docs.npmjs.com/cli/completion), I assume that you have some code in
# your .bashrc/.zshrc file. Follow the instruction below to avoid clobbering the npm autocomplete script.
#####################
#### FOR BASH change THIS (in your .bashrc file)
#####################
IFS=$'\n' COMPREPLY=($(COMP_CWORD="$cword" \
COMP_LINE="$COMP_LINE" \
COMP_POINT="$COMP_POINT" \
npm completion -- "${words[@]}" \
2>/dev/null)) || return $?
### TO THIS ( We added an if block, and wrapped the existing in the else block ) ###############################
# if your npm command includes `install`
if [[ ${words[@]} =~ 'install' ]]; 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 ) )
else
IFS=$'\n' COMPREPLY=($(COMP_CWORD="$cword" \
COMP_LINE="$COMP_LINE" \
COMP_POINT="$COMP_POINT" \
npm completion -- "${words[@]}" \
2>/dev/null)) || return $?
fi
#####################
#### FOR ZSH change THIS (in your .zshrc file)
#####################
IFS=$'\n' COMPREPLY=($(COMP_CWORD="$cword" \
COMP_LINE="$COMP_LINE" \
COMP_POINT="$COMP_POINT" \
npm completion -- "${words[@]}" \
2>/dev/null)) || return $?
### TO THIS ( We added an if block, and wrapped the existing in the else block ) ###############################
# if your npm command includes `install`
if [[ ${words} =~ 'install' ]]; then
compadd -- $(COMP_CWORD=$((CURRENT-1)) \
COMP_LINE=$BUFFER \
COMP_POINT=0 \
ls ~/.npm -- "${words[@]}" \
2>/dev/null)
else
compadd -- $(COMP_CWORD=$((CURRENT-1)) \
COMP_LINE=$BUFFER \
COMP_POINT=0 \
npm completion -- "${words[@]}" \
2>/dev/null)
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment