Emacs in the WSL (With GUI!)
I've been using emacs for around 7 years now, and in every job I've begrudgingly had Windows as the operating system. It works OK, but when I go home and fire up Linux everything just seems to work better.
I finally got round to playing around more with the Windows Subsystem for Linux (WSL), and the more I use it, the more I appreciate just how powerful it is. That said, I thought I'd give emacs a go.
Installing Emacs
The install is, as ever a dream. As long as you have your WSL enabled, and a distro installed via the Windows Store (you don't really have to log in with a Windows account, in spite of the 3 prompts to do so), you can just install using your favorite package manager.
GUI
Here's where it gets dicey. When you fire up emacs after installing it, you'll get the CLI version. If that's your jam then fine, but I wasn't going to have my colleagues and their fancy VSCode upstage me. This is where you'll need an X Server installed, which will be responsible for rendering the output from the WSL.
Initially I was using XMing, which worked well, but the fonts looked a bit dodgy and it wasn't pulling through that lovely emacs logo on the taskbar! Recently I've been using VcXsrv and those problems are solved, though with some tinkering I'm sure XMing could work just as well.
The install is simple, just grab VcXsrv here, and run the installer. Once installed, load it up and run through the config. At the end it will give you an option to save the config so you can save your settings and, more importantly, get it to run without interrupting you at startup. Once you hit go, it will start up the server.
Next up we need to instruct linux to send emacs to your running server.
export DISPLAY=:0Because I want this to run every time I fire up a new console, I've put mine in .bashrc.
And that's it! Next time you run emacs from a WSL prompt, you'll get a fully featured emacs whilst running Windows.

Open With
From windows I wanted to be able to right click and Open With emacs. It's actually pretty simple to do, but there's some things to figure out first.
How to access WSL programs from Windows
Any WSL command can be ported through with the wsl command. So we can easily open up the emacs client with the following command
wsl emacsclient ./However, that's not all we need, because file paths need to be translated from Windows to wsl with the wslpath command.
wsl emacsclient "wslpath -a ./somefile.txt"How to create the menu entry
To create a new right click menu item, we'll need to edit, possibly the worst thing about Windows, the registry. We'll need to add an folder under HKEY_CLASSES_ROOT > * > shell, called whatever you want the command to appear as. Then add another folder under that with the name "command". Finally, we'll add a default key in there, with the type REG_SZ, and a Data value of the command that we want to execute on click, in my case wsl emacsclient -n -a '' "`wslpath -a '%1'`

Now when we right click, there will be an "Open with Emacs" command that will open the file or folder in emacs.
And that's it. I'm sure I'll want to add a some more quality of life improvements as I go, but I can safely say having used this setup for about 3 months, that I've not had the same issues that I was having running the Windows version of emacs.
Comments