Parent directory

sublime.plugin.zsh

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