Last active
July 29, 2023 10:28
-
-
Save WingTillDie/1f4d70c6c12a42f903f265c98d2e0e8a to your computer and use it in GitHub Desktop.
Start vscode from WSL with UNC path
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 | |
# Start vscode from WSL with UNC path | |
# Example of such path: | |
# [ $(wslpath -w $HOME) = '\\wsl$\Ubuntu\home\'"$USER" ] | |
function get_pwd_win() { | |
local path_pwd_win=`wslpath -w $PWD` | |
echo "$path_pwd_win" | |
} | |
function get_vscode_path_win() { | |
# Handles the problem where there are multiple executables of vscode | |
local code_path=`which -a code | tail -n1` | |
local code_path_win=`wslpath -w "$code_path"` | |
echo "$code_path_win" | |
} | |
# Convert input argument via wslpath | |
function get_input_file_path_win() { | |
if [ -n "$1" ]; then | |
local input_file_path="$1" | |
local input_file_win=`wslpath -w "$input_file_path" 2> /dev/null ` | |
comment_for_input_file_win | |
if [ $? ]; then | |
# Is new file | |
input_file_win="$(get_pwd_win)\\$input_file_path" | |
fi | |
else input_file_win=''; fi | |
echo "$input_file_win" | |
} | |
# Implemented by shell functions | |
# Supports UNC path by pushd | |
function code_bash() { | |
cmd.exe /c pushd "$(get_pwd_win)" \& "$(get_vscode_path_win)" "$(get_input_file_path_win "$1")" \& popd 2> /dev/null | |
# Omits misleading erorr message by 2> /dev/null | |
# Original misleading error message example: | |
# '\\wsl$\Ubuntu\home\<USER>' | |
# CMD.EXE was started with the above path as the current directory. | |
# UNC paths are not supported. Defaulting to Windows directory. | |
} | |
function comment_for_input_file_win() { | |
# Omits misleading erorr message by 2> /dev/null | |
# Original misleading error message example: | |
# wslpath: <new_filename>: No such file or directory | |
: | |
} | |
# Same function without shell functions | |
function code_bash_alt() { | |
if [ -n "$1" ]; then | |
input_file_path="$1" | |
input_file_win=`wslpath -w "$input_file_path" 2> /dev/null ` | |
comment_for_input_file_win | |
if [ $? ]; then | |
# Is new file | |
input_file_win="$(get_pwd_win)\\$input_file_path" | |
fi | |
else input_file_win=''; fi | |
# Handles the problem where there are multiple executables of vscode | |
local code_path=`which -a code | tail -n1` | |
code_path_win=`wslpath -w "$code_path"` | |
path_pwd_win=`wslpath -w $PWD` | |
cmd.exe /c pushd "$path_pwd_win" \& "$code_path_win" "$input_file_win" \& popd 2> /dev/null | |
} | |
code_bash "$1" | |
#code_bash_alt "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment