Tips to Make WSL2 a Little More Comfortable
TL;DR
WSL2 is very easy to use and comfortable, but sometimes there are things that would make it even better if they were possible. I plan to add more tips as I find solutions.
Basics
exe Files
In WSL2, you can use Windows exe files. There are many usable exe files, but here is a summary of the ones I have used.
| Command | Purpose |
|---|---|
cmd.exe | Command Prompt |
powershell.exe | PowerShell |
clip.exe | Clipboard |
explorer.exe | File Explorer |
notepad.exe | Notepad |
write.exe | WordPad |
By using Command Prompt or PowerShell, you can bridge the WSL2 side and the Windows side. Also, clip.exe is convenient when you want to copy strings to the clipboard.
The wslpath Command
Since WSL2 paths have a different format from Windows, you need to convert them to a format that the Windows side can interpret when using these exe files.
This is easy with the wslpath command. It works as follows.
By default, it converts a Windows path to a WSL path, but on the WSL side you almost always want the reverse, so you will likely use wslpath -aw. Converting to an absolute path gives a sense of security.
Detecting Whether You Are on WSL2
When configuring dotfiles and similar settings, you may want to detect whether you are on WSL2.
I use the following approach:
Practical Use Cases
The open Command
For those who have used a Mac, this is a command that opens a file with its default application. In WSL2, you can achieve this as follows:
If you want to open it with Explorer:
Copying Strings to the Clipboard
Simply pipe strings to clip.exe.
Copying Images to the Clipboard
This requires a tricky approach. If you pass a PowerShell script as an argument to powershell.exe, it will execute it. Create the following ps1 file with any name.
imgcopy.ps1
Pasting Images from the Clipboard
This can be achieved using PowerShell's Get-Clipboard.