Skip to content

Instantly share code, notes, and snippets.

@ropnop
Created September 29, 2017 00:02
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save ropnop/10800fb5066bd5144d9aaad55a8a4d18 to your computer and use it in GitHub Desktop.
Save ropnop/10800fb5066bd5144d9aaad55a8a4d18 to your computer and use it in GitHub Desktop.
VBS Script to Launch Terminator through WSL
args = "-c" & " -l " & """DISPLAY=:0 terminator"""
WScript.CreateObject("Shell.Application").ShellExecute "bash", args, "", "open", 0
@nistath
Copy link

nistath commented Oct 11, 2018

Hey, I forked your script and made it a tad better here!
Thanks for figuring out the VBS magic.
https://gist.github.com/nistath/a9236b737b15609bc0ea6e86fe3823df

@GregRos
Copy link

GregRos commented Apr 27, 2019

Here is another improvement. Microsoft has recently introduced wsl.exe, which uses the default shell instead of always bash, and deprecated bash.exe. This version also defaults to ~ if no argument is provided.

https://gist.github.com/GregRos/6d4ad376cebe7ce1c9e52deaf90171d3

@avinoamsn
Copy link

I created a version of my own, which quietly checks to see if vsxsrv is running (starts it if not), then opens a terminator window in WSL.
https://gist.github.com/avinoamsn/495db3729d6b24ec065a710250657c16

@raneomik
Copy link

raneomik commented Oct 6, 2019

Hello, >here< is a combination of the previous solutions, adding also xServer quitting on terminator quit (if it is the only program for which xServer is needed...).
The argument check to pass it to "cd" is really nice, for example if you wish to >add terminator to the right click menu<, or launch it in IDEs embedding terminals

@raphaelmaenle
Copy link

Regarding scale issues on high dpi screens.
After changing the DPI settings to 'application' in the properties of my xserver and editing GDK_SCALE in the bashrc, terminator scales correctly when opening it via cmd.

For some reason I could not get GDK_SCALE=2 in my ~/.bashrc to work when launching terminator via the scripts in this thread.
Based on the original script I changed the args to args = "-c" & " -l " & """GDK_SCALE=2 GDK_DPI_SCALE=0.8 DISPLAY=:0 terminator""". This fixes the scaling for me - but I'd rather have it work directly the ~/.bashrc. Does anybody have a prettier solution to High DPI screens using this script?

@PauloDanielCarneiro
Copy link

Hello, >here< is a combination of the previous solutions, adding also xServer quitting on terminator quit (if it is the only program for which xServer is needed...).
The argument check to pass it to "cd" is really nice, for example if you wish to >add terminator to the right click menu<, or launch it in IDEs embedding terminals

It would be nice mentioning that, for wsl2, you can use
shell.run "C:\Windows\System32\wsl.exe bash -c ""cd " & cdPath & "; DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0 terminator""", 0, true
instead of
shell.run "C:\Windows\System32\wsl.exe bash -c ""cd " & cdPath & "; DISPLAY=:0 terminator""", 0, true

@kamermans
Copy link

@g-spacewhale I fixed the High DPI scaling issue in VcXsrv directly by setting the "High DPI scaling override" to "Application":

image

@zoloholic
Copy link

zoloholic commented Dec 19, 2020

I created a version of my own, which quietly checks to see if vsxsrv is running (starts it if not), then opens a terminator window in WSL.
https://gist.github.com/avinoamsn/495db3729d6b24ec065a710250657c16

For WSL2 - Requires Shell nameserver IP address for DISPLAY

I have edited your script to work with WSL2. Please see below if you're using WSL2

' https://medium.com/@bhupathy/install-terminator-on-windows-with-wsl-2826591d2156
set shell = WScript.CreateObject("Shell.Application")

if not IsProcessRunning("vcxsrv.exe") then
	shell.shellExecute "vcxsrv.exe", ":0 -ac -terminate -lesspointer -multiwindow -clipboard -wgl -dpi auto", "C:\Program Files\VcXsrv\", "", 0
end if
' terminator.vbs
myCd = "~"
If WScript.Arguments.Length > 0 Then
    myCd = "'$(wslpath -u '" & WScript.Arguments(0) & "')'"
End If
args = "bash" & " -c ""cd " & myCd & "; DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0 terminator"""
WScript.CreateObject("Shell.Application").ShellExecute "C:\Windows\System32\wsl.exe", args, "", "open", 0

' https://stackoverflow.com/questions/19794726/vb-script-how-to-tell-if-a-program-is-already-running
Function IsProcessRunning( strProcess )
	Dim Process, strObject
	IsProcessRunning = False
	strObject = "winmgmts://."
	For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
	If UCase( Process.name ) = UCase( strProcess ) Then
		IsProcessRunning = True
		Exit Function
	End If
	Next
End Function

@chazakki
Copy link

chazakki commented Oct 2, 2021

how do I make same kind of .vbs to run subl?

@avinoamsn
Copy link

@chazakki Are you asking about starting subl from within WSL using a similar script? You'll probably only need to change the call to WScript.CreateObject(...)..., but I couldn't say what the exact solution is. I bet that there are other examples out there that are closer to what you're looking for.

Additionally, I haven't used subl in years but VSCode runs natively in WSL using a proprietary VSCode server solution. subl may offer something similar, but I haven't looked into this myself.

@r0mdau
Copy link

r0mdau commented Jan 28, 2022

Automatically retrieve the ip address related to your WSL2 adapter using adapter name in the function GetIpFromAdapter, in VBS :

' https://gist.github.com/ropnop/10800fb5066bd5144d9aaad55a8a4d18
set shell = WScript.CreateObject("Shell.Application")

if not IsProcessRunning("vcxsrv.exe") then
	shell.shellExecute "vcxsrv.exe", ":0 -ac -terminate -lesspointer -multiwindow -clipboard -wgl -dpi auto", "C:\Program Files\VcXsrv\", "", 0
        WScript.Sleep 2000
end if

StrIP=GetIpFromAdapter("Hyper-V Virtual Ethernet Adapter #2")
shell.ShellExecute "bash", "-c -l ""DISPLAY="&StrIP &":0 terminator""", "", "open", 0

Function IsProcessRunning( strProcess )
	Dim Process, strObject
	IsProcessRunning = False
	strObject = "winmgmts://."
	For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
	If UCase( Process.name ) = UCase( strProcess ) Then
		IsProcessRunning = True
		Exit Function
	End If
	Next
End Function

Function GetIpFromAdapter( adapterName )
	dim NIC1, Nic
	Set NIC1 = GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")	
	For Each Nic in NIC1	
	    if Nic.IPEnabled And Nic.Description=adapterName then
	        GetIpFromAdapter = Nic.IPAddress(0)
	    End if
	Next
End Function

@Kahuna26
Copy link

Kahuna26 commented Feb 4, 2022

i used this script since last year. (Many thanks!) After i upgraded my windows to 11 it stopped working and i cant seem to find the problem.
Do you have any experience with terminator wsl2 and windows 11?

@pastephens
Copy link

Giant PITA!

@fahri314
Copy link

fahri314 commented Dec 9, 2023

i used this script since last year. (Many thanks!) After i upgraded my windows to 11 it stopped working and i cant seem to find the problem. Do you have any experience with terminator wsl2 and windows 11?

Windows 11 does not need this script. When you install a program in wsl, you can find windows start menu with its name and you'll see with icon if exist

@fahri314
Copy link

fahri314 commented Dec 9, 2023

I added home directory changes and more reliable IP calculation:

' https://gist.github.com/ropnop/10800fb5066bd5144d9aaad55a8a4d18
set shell = WScript.CreateObject("Shell.Application")

if not IsProcessRunning("vcxsrv.exe") then
    shell.shellExecute "vcxsrv.exe", ":0 -ac -terminate -lesspointer -multiwindow -clipboard -wgl -dpi auto", "C:\Program Files\VcXsrv\", "", 0
        WScript.Sleep 2000
end if

myCd = "~"
If WScript.Arguments.Length > 0 Then
    myCd = "'$(wslpath -u '" & WScript.Arguments(0) & "')'"
End If
args = "bash" & " -c ""cd " & myCd & " ;DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0 terminator"""
shell.ShellExecute "C:\Windows\System32\wsl.exe", args, "", "open", 0

Function IsProcessRunning( strProcess )
    Dim Process, strObject
    IsProcessRunning = False
    strObject = "winmgmts://."
    For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
    If UCase( Process.name ) = UCase( strProcess ) Then
        IsProcessRunning = True
        Exit Function
    End If
    Next
End Function

@kamermans
Copy link

Thanks @fahri314

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment