pj.plugin.zsh 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/zsh
  2. #
  3. # Original idea by DefV (Jan De Poorter)
  4. # Source: https://gist.github.com/pjaspers/368394#comment-1016
  5. #
  6. # Usage:
  7. # - Set `$PROJECT_PATHS` in your ~/.zshrc
  8. # e.g.: PROJECT_PATHS=(~/src ~/work)
  9. # - In ZSH you now can open a project directory with the command: `pj my-project`
  10. # the plugin will locate the `my-project` directory in one of the $PROJECT_PATHS
  11. # Also tab completion is supported.
  12. # - `pjo my-project` will open the directory in $EDITOR
  13. #
  14. function pj() {
  15. cmd="cd"
  16. file=$1
  17. if [[ "open" == "$file" ]] then
  18. shift
  19. file=$*
  20. cmd=(${(s: :)EDITOR})
  21. else
  22. file=$*
  23. fi
  24. for project in $PROJECT_PATHS; do
  25. if [[ -d $project/$file ]] then
  26. $cmd "$project/$file"
  27. unset project # Unset project var
  28. return
  29. fi
  30. done
  31. echo "No such project $1"
  32. }
  33. alias pjo="pj open"
  34. function _pj () {
  35. # might be possible to improve this using glob, without the basename trick
  36. typeset -a projects
  37. foreach i ($PROJECT_PATHS/*)
  38. do
  39. projects+=`basename $i`
  40. done
  41. _arguments '*:file:($projects)'
  42. }
  43. compdef _pj pj