Open WSL2 files in Windows apps using Emacs TRAMP
Contents
From the department of how-obscure-can-you-get-really, I present you with this neat trick to open WSL2 files in their native Windows handlers via Emacs TRAMP connection from WSL1 to WSL2.
Motivation
My use case is this:
- On Windows, I use Emacs primarily from WSL1 to manage everything on Windows, on WSL1 and on the WSL2 distros I use for development.
- I’m often connected to WSL2 via TRAMP, for managing files with dired, manipulating the results of simulations, and using the amazing magit.
- When I open e.g. a simulation report in HTML or some other format, I would ideally like it to open with my Windows browser.
Method
The code below is a slightly modified version of crux-open-with
from the crux
package, with the one major change that it uses the relevant TRAMP mechanisms
to spawn a remote process to open the file.
|
|
I have my special wsl version of xdg-open, xdg-open-wsl, running on WSL2 and so
triggering the function below, which I have bound to C-c o
, will open the
WSL2 file using its native Windows handler.
Conclusion
Just to reiterate, this gives you Emacs running on WSL1 displaying to Windows via X410, connected to WSL2 via TRAMP, opening WSL2 files using their native Windows handlers, at the press of a button.
Surprisingly, it feels quite integrated. ;)
If you prefer running Emacs on Windows natively, you will need to tweak the Lisp somewhat.
Let me know in the comments if you’re the one other person on this planet who found this useful!