pj.plugin.zsh 972 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. file=$2
  19. cmd=(${(s: :)EDITOR})
  20. fi
  21. for project in $PROJECT_PATHS; do
  22. if [[ -d $project/$file ]] then
  23. $cmd "$project/$file"
  24. unset project # Unset project var
  25. return
  26. fi
  27. done
  28. echo "No such project $1"
  29. }
  30. alias pjo="pj open"
  31. function _pj () {
  32. compadd `/bin/ls -l $PROJECT_PATHS 2>/dev/null | awk '{ print $9 }'`
  33. }
  34. compdef _pj pj