0

I am working with WSL and I have configured a context menu shortcut in File Explorer to launch a WSL bash terminal in the current directory. When no bash terminal is already running it works as expected. However when one window of the terminal is already running, launching a new window from another folder won't start the script in the new folder, but start the script in the same folder as where the previous instance was launched. I couldn't find a way to specify the current directory for starting the script, so the single window case simply works by default. I think I just need help figuring out the right options that would allow it to work for multiple windows.

Here is my context menu command (which I added to the registry in HKEY_CLASSES_ROOT\Directory\Background\shell)

C:\Windows\System32\wscript.exe C:\path\to\terminal.vbs

And here is the content of terminal.vbs

args = "-c" & " -l " & """DISPLAY=:0 terminator"""
WScript.CreateObject("Shell.Application").ShellExecute "bash", args, "", "open", 0

I think I just need to specify an option for ShellExecute that will launch it in the working directory, right now it seems like the single window case just works by default.

BACKGROUND: I followed this guide to set up the terminal in WSL. https://blog.ropnop.com/configuring-a-pretty-and-usable-terminal-emulator-for-wsl/

Note: This is my first stackexchange post and I am not too familiar with the windows command line ecosystem. I'll take suggestions on how to make the title of the post more relevant. Thank you.

Adrien
  • 1

1 Answers1

0

The following procedure uses batch file and does not use any Visual Basic script because some anti-virus programs may catch those as malware. Hence there will be Command Prompt window in background.

Procedure

In WSL:

These are tested with Debian in Windows Subsystem for Linux (WSL). Some steps may not require in different distribution.

  • Install required packages:
apt update
apt upgrade
apt install terminator dbus-x11
  • Add some files to suppress warnings:
mkdir -p ~/.config/terminator
touch ~/.config/terminator/config

In Windows:

  • Run the X server in Windows. For example, Xming, VcxSrv, X410 etc.

  • Make a batch file (example, test.bat) with these following lines:

@echo off
for /F %%X in (
'wsl.exe wslpath %1') do (
wsl.exe DISPLAY=:0 terminator --working-directory="%%X"
)

Let assume this batch file path is C:\MyFiles\test.bat. Then make a registry file (example, test.reg) with these following lines:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\Open Terminator Here\command]
@="C:\\MyFiles\\test.bat \"%V\""

Now double click on the test.reg file and accept the warnings. This will import the registry. Right click in any folder there will be a context menu "Open Terminator Here".


Explanation

In the registry "%V" placeholder variable takes the current working directory path. See this answer for further details. This folder path is passed to batch file. In the batch file, %1 placeholder variable takes that folder path. wsl.exe wslpath command converts the Windows style path to UNIX style. Now this UNIX style path is passed to DISPLAY=:0 terminator --working-directory command with the help of for /F loop and %X variable. Also the DISPLAY variable can be changed as you need.

Biswapriyo
  • 11,584