Search code examples
windowsx11windows-subsystem-for-linux

How to elegantly create a Windows shortcut that starts a WSL program under X


I'm struggling a little to launch xfce4-terminal from my WSL installation under VcXsrv from a button on the Windows taskbar. I don't want any DOS box/console/terminal other than the xfce4-terminal when I'm done. I have it working, but man is it ugly. Does anybody have a cleaner way of doing this?

Here is what I got working: I created a windows shortcut on the Desktop with this target (all in one line, broken with newlines for readability here):

C:\Windows\System32\wscript.exe
    \\wsl$\Ubuntu-20.04\home\peter\bin\windows\startTerminal.vbs

startTerminal.vbs was inspired by from 10 Ways To Run Batch Files Silently And Hide The Console Window • Raymond.CC (one of the few solutions that didn't require installing a separate program for this!) and it contains:

CreateObject("Wscript.Shell").Run "C:\Windows\System32\wsl.exe -u peter --exec /home/peter/bin/windows/startTerminal.sh",0,True

and startTerminal.sh contains:

export DISPLAY=localhost:0.0
xfce4-terminal --command=/bin/zsh

Setting DISPLAY is apparently required, even though I set the DISPLAY environment in ~/.zshrc. wsl.exe apparently doesn't do that unless you run the login shell.

Once all of this is working, I can drag my shortcut to the taskbar and click on it there.

I count 3, three files cooperating to achieve this simple goal? Can I limit that to one or two without installing an external program?


Solution

  • I removed the need for a startTerminal.sh file by:

    Editing startTerminal.vbs to contain:

    CreateObject("Wscript.Shell").Run "C:\Windows\System32\wsl.exe -u peter bash -l -c xfce4-terminal",0,True
    

    And then I created a ~/.bash_profile to set the $DISPLAY variable, which probably should've been done anyway:

    $ cat .bash_profile
    if [ -z "$DISPLAY" ]; then 
        export DISPLAY=:0
    fi
    

    So now there is the shortcut and a startTerminal.vbs that starts it up. It still isn't very elegant...