Skip to content

Instantly share code, notes, and snippets.

@evadne evadne/script.sh
Created Apr 28, 2019

Embed
What would you like to do?
Properly invoke Sublime Text from WSL
function subl () {
SublimePath="/mnt/c/Program Files/Sublime Text 3/subl.exe"
TargetPath=$(readlink -f $1)
HomePath="/home/evadne"
ProjectsPath=$(readlink -f $HomePath/projects)
SublimeProjectsPath=$(readlink -f $HomePath/sublime)
echo "Editing $TargetPath"
case $TargetPath/ in
$ProjectsPath/*)
ProjectNamespaceComponents=$(realpath --relative-to="$ProjectsPath" "$TargetPath")
if [[ "$ProjectNamespaceComponents" =~ '^([^/]+)/([^/]+)(.+)?$' ]] {
ProjectDirectory="$ProjectsPath/$match[1]/$match[2]"
SublimeProjectDirectory="$SublimeProjectsPath/projects/$match[1]"
mkdir -p $SublimeProjectDirectory
SublimeProjectName="$match[2].sublime-project"
SublimeProjectFolderPath=$(realpath --relative-to="$SublimeProjectDirectory" "$ProjectDirectory")
}
;;
$HomePath/*)
ProjectDirectory=$HomePath
SublimeProjectDirectory="$SublimeProjectsPath"
SublimeProjectName="home.sublime-project"
SublimeProjectFolderPath='\\\\wsl$\\Ubuntu\\home\\evadne'
;;
*/)
ProjectDirectory="/"
SublimeProjectDirectory="$SublimeProjectsPath"
SublimeProjectName="root.sublime-project"
SublimeProjectFolderPath='\\\\wsl$\\Ubuntu'
;;
esac
SublimeProjectPath="$SublimeProjectDirectory/$SublimeProjectName"
printf '{"folders": [{"path": "%s"}]}' "$SublimeProjectFolderPath" > "$SublimeProjectPath"
cat $SublimeProjectPath
pushd $SublimeProjectDirectory
$SublimePath --new-window --project $SublimeProjectName
popd
}
➜ ~ ls -al | grep mnt
lrwxrwxrwx 1 evadne evadne 15 Apr 26 14:36 projects -> /mnt/d/Projects
lrwxrwxrwx 1 evadne evadne 19 Apr 28 19:13 sublime -> /mnt/d/Sublime Text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.