sublime.plugin.zsh 3.8 KB

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