npm.plugin.zsh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. (( $+commands[npm] )) && {
  2. rm -f "${ZSH_CACHE_DIR:-$ZSH/cache}/npm_completion"
  3. _npm_completion() {
  4. local si=$IFS
  5. compadd -- $(COMP_CWORD=$((CURRENT-1)) \
  6. COMP_LINE=$BUFFER \
  7. COMP_POINT=0 \
  8. npm completion -- "${words[@]}" \
  9. 2>/dev/null)
  10. IFS=$si
  11. }
  12. compdef _npm_completion npm
  13. }
  14. # Install dependencies globally
  15. alias npmg="npm i -g "
  16. # npm package names are lowercase
  17. # Thus, we've used camelCase for the following aliases:
  18. # Install and save to dependencies in your package.json
  19. # npms is used by https://www.npmjs.com/package/npms
  20. alias npmS="npm i -S "
  21. # Install and save to dev-dependencies in your package.json
  22. # npmd is used by https://github.com/dominictarr/npmd
  23. alias npmD="npm i -D "
  24. # Force npm to fetch remote resources even if a local copy exists on disk.
  25. alias npmF='npm i -f'
  26. # Execute command from node_modules folder based on current directory
  27. # i.e npmE gulp
  28. alias npmE='PATH="$(npm bin)":"$PATH"'
  29. # Check which npm modules are outdated
  30. alias npmO="npm outdated"
  31. # Update all the packages listed to the latest version
  32. alias npmU="npm update"
  33. # Check package versions
  34. alias npmV="npm -v"
  35. # List packages
  36. alias npmL="npm list"
  37. # List top-level installed packages
  38. alias npmL0="npm ls --depth=0"
  39. # Run npm start
  40. alias npmst="npm start"
  41. # Run npm test
  42. alias npmt="npm test"
  43. # Run npm scripts
  44. alias npmR="npm run"
  45. # Run npm publish
  46. alias npmP="npm publish"
  47. # Run npm init
  48. alias npmI="npm init"
  49. # Run npm info
  50. alias npmi="npm info"
  51. # Run npm search
  52. alias npmSe="npm search"
  53. npm_toggle_install_uninstall() {
  54. # Look up to the previous 2 history commands
  55. local line
  56. for line in "$BUFFER" \
  57. "${history[$((HISTCMD-1))]}" \
  58. "${history[$((HISTCMD-2))]}"
  59. do
  60. case "$line" in
  61. "npm uninstall"*)
  62. BUFFER="${line/npm uninstall/npm install}"
  63. (( CURSOR = CURSOR + 2 )) # uninstall -> install: 2 chars removed
  64. ;;
  65. "npm install"*)
  66. BUFFER="${line/npm install/npm uninstall}"
  67. (( CURSOR = CURSOR + 2 )) # install -> uninstall: 2 chars added
  68. ;;
  69. "npm un "*)
  70. BUFFER="${line/npm un/npm install}"
  71. (( CURSOR = CURSOR + 5 )) # un -> install: 5 chars added
  72. ;;
  73. "npm i "*)
  74. BUFFER="${line/npm i/npm uninstall}"
  75. (( CURSOR = CURSOR + 8 )) # i -> uninstall: 8 chars added
  76. ;;
  77. *) continue ;;
  78. esac
  79. return 0
  80. done
  81. BUFFER="npm install"
  82. CURSOR=${#BUFFER}
  83. }
  84. zle -N npm_toggle_install_uninstall
  85. # Defined shortcut keys: [F2] [F2]
  86. bindkey -M emacs '^[OQ^[OQ' npm_toggle_install_uninstall
  87. bindkey -M vicmd '^[OQ^[OQ' npm_toggle_install_uninstall
  88. bindkey -M viins '^[OQ^[OQ' npm_toggle_install_uninstall