Last Updated: February 25, 2016
·
731
· xcambar

Auto-completes the npm executables in ZSH

Node.js modules sometimes come with handy executables, available under ./node_modules/.bin.

One way to access them is by doing (as en example);

$ `npm bin`/the_executable_name

With the following code, to be added in your ZSH configuration, you gain autocomplete for these executables through an npmbin command.

No more need to hurt your pinky finger going up to those backticks, hooray!

Here's the code

#
# Auto completes the executables located in node_modules/.bin 
#
function npmbin () {
  PATH=$(npm bin):$PATH $@
}
function _comp_npm () {
  _arguments '1: :->command_'
  case $state in
    command_)
      _files -W "$(npm bin)" -/
      ;;
    *)
      _files
      ;;
  esac
}
compdef _comp_npm npmbin

Have fun!