Last active
December 28, 2021 09:54
-
-
Save maddouri/de3445b2d4036215e3c578770bb6da1b to your computer and use it in GitHub Desktop.
Bash function for calling Windows' Sublime Text from WSL2 (supports command line arguments and path translation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Calls Windows' Sublime Text from WSL2 | |
# | |
# Usage: subl [arguments] [files] Edit the given files | |
# or: subl [arguments] [directories] Open the given directories | |
# or: subl [arguments] -- [files] Edit files that may start with '-' | |
# or: subl [arguments] - Edit stdin | |
# or: subl [arguments] - >out Edit stdin and write the edit to stdout | |
# | |
# Arguments: | |
# --project <project>: Load the given project | |
# --command <command>: Run the given command | |
# -n or --new-window: Open a new window | |
# --launch-or-new-window: Only open a new window if the application is open | |
# -a or --add: Add folders to the current window | |
# -w or --wait: Wait for the files to be closed before returning | |
# -b or --background: Don't activate the application | |
# -s or --stay: Keep the application activated after closing the file | |
# --safe-mode: Launch using a sandboxed (clean) environment | |
# -h or --help: Show help (this message) and exit | |
# -v or --version: Show version and exit | |
# | |
# Filenames may be given a :line or :line:column suffix to open at a specific | |
# location. | |
# | |
subl() { | |
local SUBL_0_ARGS=( | |
'-n' '--new-window' | |
'--launch-or-new-window' | |
'-a' '--add' | |
'-w' '--wait' | |
'-b' '--background' | |
'-s' '--stay' | |
'--safe-mode' | |
'-h' '--help' | |
'-v' '--version' | |
'--' | |
'-' | |
'>out' | |
) | |
local SUBL_1_ARGS=( | |
'--project' | |
'--command' | |
) | |
local APPEND_1_ARGS=false | |
local IS_PATH=false | |
local ARGS=() | |
local ARG | |
# loop over positional params https://stackoverflow.com/a/1769202/865719 | |
for ARG ; do | |
# path or another argument? | |
if [[ "${APPEND_REMAINING_PATHS}" == true ]] ; then # -- remaining paths | |
IS_PATH=true | |
elif [[ "${APPEND_1_ARGS}" == true ]] ; then # arg of an arg? | |
ARGS+=("(${ARG})") | |
APPEND_1_ARGS=false | |
elif [[ " ${SUBL_0_ARGS[@]} " =~ " ${ARG} " ]] ; then # 1 arg? https://stackoverflow.com/a/15394738/865719 | |
ARGS+=("${ARG}") | |
elif [[ " ${SUBL_1_ARGS[@]} " =~ " ${ARG} " ]] ; then # 2 args? https://stackoverflow.com/a/15394738/865719 | |
ARGS+=("${ARG}") | |
APPEND_1_ARGS=true | |
else # path! convert it! | |
IS_PATH=true | |
fi | |
# process paths | |
if [[ "${IS_PATH}" == true ]] ; then | |
local WSL_PATH="${ARG}" | |
# has :line[:col] suffix? | |
local LINE_COL="$(grep -Poh '(:[0-9]+){1,2}$' <<< "${ARG}")" | |
if [[ -n "${LINE_COL}" ]] ; then | |
# remove the :line[:col] suffix | |
WSL_PATH="$(sed -E "s|^(.*)${LINE_COL}$|\\1|" <<< "${ARG}")" | |
fi | |
local WIN_PATH="$(wslpath -a -w "${WSL_PATH}")" | |
ARGS+=("${WIN_PATH}${LINE_COL}") | |
IS_PATH=false | |
fi | |
done | |
# done! | |
local SUBL_WIN_PATH='C:\\Program Files\\Sublime Text\\subl.exe' | |
"$(wslpath -a -u "${SUBL_WIN_PATH}")" "${ARGS[@]}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment