sublime.plugin.zsh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "/snap/bin/subl"
  27. "/snap/bin/sublime-text.subl"
  28. )
  29. fi
  30. elif [[ "$OSTYPE" = darwin* ]]; then
  31. _sublime_paths=(
  32. "/usr/local/bin/subl"
  33. "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl"
  34. "/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
  35. "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl"
  36. "$HOME/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl"
  37. "$HOME/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
  38. "$HOME/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl"
  39. )
  40. elif [[ "$OSTYPE" = cygwin ]]; then
  41. _sublime_paths=(
  42. "$(cygpath "$ProgramW6432/Sublime Text 2")/subl.exe"
  43. "$(cygpath "$ProgramW6432/Sublime Text 3")/subl.exe"
  44. )
  45. elif [[ "$OSTYPE" = msys ]]; then
  46. _sublime_paths=(
  47. "/c/Program Files/Sublime Text 2/subl.exe"
  48. "/c/Program Files/Sublime Text 3/subl.exe"
  49. )
  50. fi
  51. for _sublime_path in $_sublime_paths; do
  52. if [[ -a $_sublime_path ]]; then
  53. alias subl="'$_sublime_path'"
  54. (( $+commands[sudo] )) && alias sst="sudo '$_sublime_path'"
  55. break
  56. fi
  57. done
  58. unset _sublime_paths _sublime_path
  59. }
  60. function find_project() {
  61. local PROJECT_ROOT="${PWD}"
  62. local FINAL_DEST="."
  63. while [[ $PROJECT_ROOT != "/" && ! -d "$PROJECT_ROOT/.git" ]]; do
  64. PROJECT_ROOT=$(dirname $PROJECT_ROOT)
  65. done
  66. if [[ $PROJECT_ROOT != "/" ]]; then
  67. local PROJECT_NAME="${PROJECT_ROOT##*/}"
  68. local SUBL_DIR=$PROJECT_ROOT
  69. while [[ $SUBL_DIR != "/" && ! -f "$SUBL_DIR/$PROJECT_NAME.sublime-project" ]]; do
  70. SUBL_DIR=$(dirname $SUBL_DIR)
  71. done
  72. if [[ $SUBL_DIR != "/" ]]; then
  73. FINAL_DEST="$SUBL_DIR/$PROJECT_NAME.sublime-project"
  74. else
  75. FINAL_DEST=$PROJECT_ROOT
  76. fi
  77. fi
  78. subl $FINAL_DEST
  79. }
  80. function create_project() {
  81. local _target=$1
  82. if [[ "${_target}" == "" ]]; then
  83. _target=$(pwd);
  84. elif [[ ! -d ${_target} ]]; then
  85. echo "${_target} is not a valid directory"
  86. return 1
  87. fi
  88. local _sublime_project_file=$_target/$(basename $_target).sublime-project
  89. if [[ ! -f $_sublime_project_file ]]; then
  90. touch $_sublime_project_file
  91. echo -e "{" >> $_sublime_project_file
  92. echo -e "\t\"folders\":" >> $_sublime_project_file
  93. echo -e "\t\t[{" >> $_sublime_project_file
  94. echo -e "\t\t\t\"path\": \".\"," >> $_sublime_project_file
  95. echo -e "\t\t\t\"file_exclude_patterns\": []" >> $_sublime_project_file
  96. echo -e "\t\t}]" >> $_sublime_project_file
  97. echo -e "}" >> $_sublime_project_file
  98. echo -e "New Sublime Text project created:\n\t${_sublime_project_file}"
  99. fi
  100. }