11

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?

Peter V. Mørch
  • 13,830
  • 8
  • 69
  • 103

4 Answers4

10

Yes, I think you can do what I do in one shortcut file. I'll share what works for me then you'll have to fiddle for your program.

Try putting all your commands in the Target section of the Shortcut link file.

Step 1. Right click on desktop then New then Shortcut then name it (LaunchXFCE4)

Step 2. Right click LaunchXFCE4 select Properties

Step 3. Enter all your launch sequences in the Target section

Step 4. Right click LaunchXFCE4 select Pin to Taskbar

I'm launching Emacs 27.1 on WSL 2 with Ubuntu 20.10 on Windows 10 using this code which perhaps you can modify.

C:\Windows\System32\wsl.exe --distribution Ubuntu20 bash -c "export DISPLAY=$(ip route | awk '{print $3; exit}'):0 && export LIBGL_ALWAYS_INDIRECT=1 && export XCURSOR_SIZE=16 && setsid emacs"

The first export display is similar to your localhost:0.0 so replace it or try mine. Maybe your xfce4 replaces my emacs. Read this post about double ampersands. So possibly this

C:\Windows\System32\wsl.exe --distribution Ubuntu20 bash -c "export DISPLAY=localhost:0.0 && export LIBGL_ALWAYS_INDIRECT=1 && export XCURSOR_SIZE=16 && setsid xfce4-terminal --command=/bin/zsh"

Note, my VcXsrv starts when windows starts by putting a shortcut to config.xlaunch in my C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.

By the way, Microsoft will provide a GUI for WSL in a few months, according to their blog

GUI app support in WSL is becoming a reality! We are getting closer to an initial preview and happy to announce a preview release for Windows Insiders within the next couple of months.

Saj
  • 781
  • 8
  • 7
  • 1
    Thanks, @saj for your suggestion. When I try it I get the xfce-terminal, but also the "dos box" as shown here: https://imgur.com/30Nmi90. I need that dos box to not appear. Also, we all know what "within the next couple of months" means... A year? :-) – Peter V. Mørch Nov 17 '20 at 21:31
  • You're welcome Peter. I don't have any extra windows/dos boxes when I use this. So you'll have to fiddle with the parameters in that code or in the VcXsrv launch config. – Saj Nov 19 '20 at 00:12
  • It just occurred to me that perhaps you need to set the Run field to Minimized in your shortcut that launches config.xlaunch, which should be in your user > Start Menu > Programs > Startup folder. – Saj Jan 29 '21 at 02:51
  • Then the dos box will be minimized but still in the task bar. I need it not to be there a all. – Peter V. Mørch Jan 29 '21 at 06:31
  • 1
    Thank you! I used this to start alacritty with tabbed: C:\Windows\System32\wsl.exe --distribution Ubuntu bash -c "export DISPLAY=localhost:0.0 && export LIBGL_ALWAYS_INDIRECT=1 && setsid tabbed -g 2300x1800 -c alacritty --working-directory ~ --embed" The only catch is that only Cygwin X server works (VcXsrv doesn't) and trying to make the geometry bigger causes it to close itself for some reason. – Emilio Dec 29 '21 at 14:16
  • @PeterV.Mørch were you able to get rid of the DOS box eventually? – Bobbbay Aug 01 '22 at 08:33
  • @Bobbbay: Did you read my original post and/or my answer? I just didn't succeed to get it working with *this* answer. – Peter V. Mørch Aug 12 '22 at 05:39
  • @PeterV.Mørch right, so the answer to my question is no, you weren't able to get rid of the DOS box with this solution. Thank you. – Bobbbay Aug 17 '22 at 16:22
3

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...

Peter V. Mørch
  • 13,830
  • 8
  • 69
  • 103
  • Apparently, `export DISPLAY=localhost:0.0` used to work. But on a current Windows 10 installation, I've had to change that to `export DISPLAY=:0` to get it to work. I have no idea why... – Peter V. Mørch Jul 01 '21 at 14:56
  • 1
    AFAICT, DISPLAY=localhost:0.0 connects to the X server over TCP/IP, whereas DISPLAY=:0 connects to the X server using a socket. They both should work, but... – Bryan Larsen Aug 31 '21 at 15:49
  • Thanks, @BryanLarsen: That totally makes sense. I'm now using [wsld](https://github.com/nbdd0121/wsld) to be more resilient to Windows networking changes so now I *am* using a (v)socket (whatever that is). – Peter V. Mørch Aug 31 '21 at 22:36
1

After experimenting with @Saj 's answer which did not work for me I resulted in the below:

C:\Windows\System32\wsl.exe --distribution Ubuntu-20.04 bash -c "export DISPLAY=$((route.exe print | Select-String 0.0.0.0 | select -first 1) -Split "\s+" | select -Index 4):0.0 && export LIBGL_ALWAYS_INDIRECT=1 && setsid emacs"

Note that my problem was that it could not use bash commands inside the parenthesis (grep, awk)

Environment: WSL2 on Windows 10

  • This works pretty well. Just a note about the `LIBGL_ALWAYS_INDIRECT=1` - some applications don't need that set, for instance, opening `alacritty` only works without that option. – mnestorov May 12 '23 at 08:13
1

One of the few good additions to Windows was WSL-2, it's much more capable now. It's part of late Win 10 and Win 11. Here is how to elegently create a terminal window, replace the terminal with any X app you want.

  1. Linux: "apt-get install xfce4-terminal" (or equivalent)
  2. Windows: Create a new Shortcut (right click in any windows folder) 2.1) Shortcut to -> C:\Windows\System32\wslg.exe -u root -- /usr/bin/xfce4-terminal If you have multiple distributions use it like: "-d Debian"

That's it. You can give it a better icon and name it. It will launch the application as if it was a windows application.

John
  • 7,507
  • 3
  • 52
  • 52