Created
June 27, 2025 09:51
-
-
Save Johnpc123/adb23a2b3bb4f69fd400612c738af223 to your computer and use it in GitHub Desktop.
WSL aware VSCode (flavor) launcher
This file contains hidden or 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
| #!/bin/bash | |
| # | |
| # ============================================================================== | |
| # WSL aware VSCode (flavor) launcher | |
| # ============================================================================== | |
| # | |
| # This resolves a very disruptive workflow issue where, even when using a WSL terminal directly, VScode projects open in a windows | |
| # environment by default. Making it necessary to use the --remote flag, and to | |
| # specify the remote, or to click through the menus to reopen the project in WSL. | |
| # | |
| # This script is a tool to automatically open a specified code editor, be that VSCode, Codium, Windsurf, | |
| # or any other VSCode derivitave you would like, in WSL if you are using windows. | |
| # | |
| # --- How to Reference from .bashrc --- | |
| # | |
| # To use this script, you create simple aliases in your shell's configuration | |
| # file (e.g., ~/.bashrc). An alias is just a shortcut for a longer command. | |
| # | |
| # alias <your-shortcut>="<path_to_this_script> <editor> <project_path>" | |
| # | |
| # as is normal, restart your shell or source ~/.bashrc | |
| # | |
| # --- Example Alias --- | |
| # | |
| # alias dev-api="~/.local/bin/open-with-wsl.sh codium ~/dev/my-api" | |
| # | |
| # Now, you can just type 'dev-api' in your terminal to open your VSCode in WSL. | |
| # | |
| # ============================================================================== | |
| # --- Step 1: Validate Inputs --- | |
| # $1: The editor command (e.g., 'codium', 'code', 'windsurf') | |
| # $2: The path to the project directory | |
| EDITOR_CMD=$1 | |
| PROJECT_DIR=$2 | |
| # Check if both arguments were provided. | |
| if [[ -z "$EDITOR_CMD" || -z "$PROJECT_DIR" ]]; then | |
| echo "Usage: $(basename "$0") <editor_command> <path_to_project_directory>" | |
| echo "Example from alias: $(basename "$0") codium ~/my-project" | |
| exit 1 | |
| fi | |
| # Check if the specified editor command actually exists and is executable. | |
| if ! command -v "$EDITOR_CMD" &> /dev/null; then | |
| echo "Error: Editor command '$EDITOR_CMD' not found." | |
| echo "Please make sure it's installed and in your PATH." | |
| exit 1 | |
| fi | |
| # Check if the provided project directory exists. | |
| if [[ ! -d "$PROJECT_DIR" ]]; then | |
| echo "Error: Directory '$PROJECT_DIR' not found." | |
| exit 1 | |
| fi | |
| # --- Step 2: Update Git Repository --- | |
| echo "Updating repository in '$PROJECT_DIR'..." | |
| env -C "$PROJECT_DIR" git pull || exit 1 | |
| # --- Step 3: Find the Workspace File --- | |
| # Automatically find a .code-workspace file in the project directory. | |
| WORKSPACE_FILE=$(find "$PROJECT_DIR" -maxdepth 1 -name "*.code-workspace" -print -quit) | |
| if [[ -z "$WORKSPACE_FILE" ]]; then | |
| echo "No .code-workspace file found. Opening the directory..." | |
| TARGET_PATH="$PROJECT_DIR" | |
| else | |
| echo "Found workspace: $WORKSPACE_FILE" | |
| TARGET_PATH="$WORKSPACE_FILE" | |
| fi | |
| # --- Step 4: Environment-Specific Launch --- | |
| # This logic uses the validated $EDITOR_CMD variable to launch the correct editor. | |
| if [[ -n "$WSL_DISTRO_NAME" ]]; then | |
| # WSL Environment | |
| echo "WSL environment detected. Opening with '$EDITOR_CMD' in WSL context..." | |
| "$EDITOR_CMD" --remote "wsl+$WSL_DISTRO_NAME" "$TARGET_PATH" | |
| else | |
| # Native Linux Environment | |
| echo "Native Linux environment detected. Opening with '$EDITOR_CMD'..." | |
| "$EDITOR_CMD" "$TARGET_PATH" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment