Skip to content

Instantly share code, notes, and snippets.

@connorjan
Last active July 23, 2017 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save connorjan/9b5083f97eb8fd28bd7652f404b9fee3 to your computer and use it in GitHub Desktop.
Save connorjan/9b5083f97eb8fd28bd7652f404b9fee3 to your computer and use it in GitHub Desktop.
Using Sublime Text on Bash on Windows

Using Sublime Text on Bash on Windows

  1. First make sure to install rsub in Bash (follow the tutorial here) and make sure it is in the $PATH environment variable.

  2. Create a symlink to the windows executable of Sublime Text so it is the $PATH (I use the ~/bin/ directory that I created). I did this by adding the following lines to my ~/.bashrc because I have found symlinks to be buggy in the Bash environment. This ensures the symlink is re-created each time Bash starts up. (Note: your installation path to the Sublime Text executable may vary)

    $ ln -sfn "$HOME/bin/wsub" "/mnt/d/Program Files/Sublime Text 3/subl.exe"
  3. Create a new file in ~/bin called subl and paste the code from the subl.sh file located below this tutorial.

  4. Give the script permissions to run:

    $ sudo chmod u+x ~/bin/subl
  5. ?

  6. PROFIT

#!/bin/bash
# Open wsub or rsub depending on the file path
lfiles=()
wfiles=()
for file in "$@"; do
fullPath="$(readlink -f $file)" # Get the full path of each argument
if [[ $fullPath == /mnt/* ]]; then
# Keep track of files that Windows has access to
fullPath="${fullPath#/mnt/}" # Remove the /mnt/ from the path
fullPath="${fullPath^}" # make the drive letter capital
# Convert to Windows style path string
# Thanks: http://stackoverflow.com/questions/13701218/windows-path-to-posix-path-conversion-in-bash
fullPath="$(echo "$fullPath" | sed -e 's/^\///' -e 's/\//\\/g' -e 's/^./\0:/')"
wfiles+=("$fullPath")
else
# Keep track of files only Linux can see
lfiles+=("$file")
fi
done
if [ ${#wfiles[@]} -eq 0 ] && [ ${#lfiles[@]} -eq 0 ]; then
# If no files were specified just open up sublime
wsub 2>/dev/null
exit $?
fi
if [ ${#wfiles[@]} -ne 0 ]; then
# If there are any windows files to open
for file in "${wfiles[@]}"; do
wsub "$file" 2>/dev/null # Open a Windows file
done
fi
if [ ${#lfiles[@]} -ne 0 ]; then
# If there are linux file we need to check if sublime is open for rsub to work
if [[ "$(tasklist.exe 2>/dev/null | grep sublime_text.exe)" == "" ]]; then
# Sublime isn't open so open it
wsub 2>/dev/null &
sleep 2 # Wait for sublime to open otherwise rsub will not open
fi
for file in "${lfiles[@]}"; do
rsub "$file" # Open a Linux file through rsub
done
# Force explorer to focus sublime text
wsub 2>/dev/null
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment