sublime.plugin.zsh 3.7 KB

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