sublime.plugin.zsh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Sublime Text aliases
  2. alias st=subl
  3. alias stt='subl .'
  4. # Define sst only if sudo exists
  5. (( $+commands[sudo] )) && alias sst='sudo subl'
  6. alias stp=find_project
  7. alias stn=create_project
  8. # Search for the Sublime Text command if not found
  9. (( $+commands[subl] )) || {
  10. declare -a _sublime_paths
  11. if [[ "$OSTYPE" == linux* ]]; then
  12. if [[ "$(uname -r)" = *icrosoft* ]]; then
  13. _sublime_paths=(
  14. "$(wslpath -u 'C:\Program Files\Sublime Text 3\subl.exe' 2>/dev/null)"
  15. "$(wslpath -u 'C:\Program Files\Sublime Text 2\subl.exe' 2>/dev/null)"
  16. )
  17. else
  18. _sublime_paths=(
  19. "$HOME/bin/sublime_text"
  20. "/opt/sublime_text/sublime_text"
  21. "/opt/sublime_text_3/sublime_text"
  22. "/usr/bin/sublime_text"
  23. "/usr/local/bin/sublime_text"
  24. "/usr/bin/subl"
  25. "/usr/bin/subl3"
  26. )
  27. fi
  28. elif [[ "$OSTYPE" = darwin* ]]; then
  29. _sublime_paths=(
  30. "/usr/local/bin/subl"
  31. "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl"
  32. "/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
  33. "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl"
  34. "$HOME/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl"
  35. "$HOME/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
  36. "$HOME/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl"
  37. )
  38. elif [[ "$OSTYPE" = cygwin ]]; then
  39. _sublime_paths=(
  40. "$(cygpath "$ProgramW6432/Sublime Text 2")/subl.exe"
  41. "$(cygpath "$ProgramW6432/Sublime Text 3")/subl.exe"
  42. )
  43. elif [[ "$OSTYPE" = msys ]]; then
  44. _sublime_paths=(
  45. "/c/Program Files/Sublime Text 2/subl.exe"
  46. "/c/Program Files/Sublime Text 3/subl.exe"
  47. )
  48. fi
  49. for _sublime_path in $_sublime_paths; do
  50. if [[ -a $_sublime_path ]]; then
  51. alias subl="'$_sublime_path'"
  52. (( $+commands[sudo] )) && alias sst="sudo '$_sublime_path'"
  53. break
  54. fi
  55. done
  56. unset _sublime_paths _sublime_path
  57. }
  58. function find_project() {
  59. local PROJECT_ROOT="${PWD}"
  60. local FINAL_DEST="."
  61. while [[ $PROJECT_ROOT != "/" && ! -d "$PROJECT_ROOT/.git" ]]; do
  62. PROJECT_ROOT=$(dirname $PROJECT_ROOT)
  63. done
  64. if [[ $PROJECT_ROOT != "/" ]]; then
  65. local PROJECT_NAME="${PROJECT_ROOT##*/}"
  66. local SUBL_DIR=$PROJECT_ROOT
  67. while [[ $SUBL_DIR != "/" && ! -f "$SUBL_DIR/$PROJECT_NAME.sublime-project" ]]; do
  68. SUBL_DIR=$(dirname $SUBL_DIR)
  69. done
  70. if [[ $SUBL_DIR != "/" ]]; then
  71. FINAL_DEST="$SUBL_DIR/$PROJECT_NAME.sublime-project"
  72. else
  73. FINAL_DEST=$PROJECT_ROOT
  74. fi
  75. fi
  76. subl $FINAL_DEST
  77. }
  78. function create_project() {
  79. local _target=$1
  80. if [[ "${_target}" == "" ]]; then
  81. _target=$(pwd);
  82. elif [[ ! -d ${_target} ]]; then
  83. echo "${_target} is not a valid directory"
  84. return 1
  85. fi
  86. local _sublime_project_file=$_target/$(basename $_target).sublime-project
  87. if [[ ! -f $_sublime_project_file ]]; then
  88. touch $_sublime_project_file
  89. echo -e "{" >> $_sublime_project_file
  90. echo -e "\t\"folders\":" >> $_sublime_project_file
  91. echo -e "\t\t[{" >> $_sublime_project_file
  92. echo -e "\t\t\t\"path\": \".\"," >> $_sublime_project_file
  93. echo -e "\t\t\t\"file_exclude_patterns\": []" >> $_sublime_project_file
  94. echo -e "\t\t}]" >> $_sublime_project_file
  95. echo -e "}" >> $_sublime_project_file
  96. echo -e "New Sublime Text project created:\n\t${_sublime_project_file}"
  97. fi
  98. }