WSL icon indicating copy to clipboard operation
WSL copied to clipboard

Reliable way to set a WSL2 application to be the default for opening a filetype

Open mangelozzi opened this issue 5 years ago • 5 comments

Is your feature request related to a problem? Please describe. I use Neovim (Vim) as my text editor. It is installed in a WSL2 Ubuntu 20.04 instance. I would like it to be my default editor in windows for text files (e.g. If one double clicks a text file in explorer). I create .bat files to achieve, but they came seeming to break when they are windows updates.

Describe the solution you'd like Someway to asscoiate file types with WSL applications, and correctly pass the file paths to them.

Describe alternatives you've considered Long convoluded bash scripts, that work then stop working. e.g. if you require a double quote in the bash command, need to escape it with \\"^""

Additional context Here is the script I created https://stackoverflow.com/a/62876682/5506400 (been viewed about 1k times).

mangelozzi avatar Oct 15 '21 12:10 mangelozzi

For temporary use there is an app in store called openinwsl that works by right click context menu and enables association of wsl apps by file exts.

hemanta212 avatar Oct 19 '21 10:10 hemanta212

For temporary use there is an app in store called openinwsl that works by right click context menu and enables association of wsl apps by file exts.

Unfortunately openinwsl is only for graphical programs.

mangelozzi avatar Oct 19 '21 10:10 mangelozzi

This is a niche use case but there are 2 reliable options already which will not break from a windows update, do not need a special execution policy and don't involve any bash scripts at all. I am going to gloss over basic steps since this is an issue that would likely only affect developers.

  1. Create a shortcut. Set the target to %SystemRoot%\System32\wsl.exe -e nvim "%1". Place the shortcut somewhere in your windows PATH. Associate your file types with that shortcut.

  2. Create a simple batch script (similar to your solution). Place it in a location in your windows PATH. Right click a .txt file or whatever you are trying to open in vim and associate it with your batch file.

@echo off
wsl nvim %*

Personally I don't like to use vim in cmd.exe so I would open it in windows terminal by adding to the bat file.

@echo off
wt.exe -d . wsl nvim %*

Similarly if you want to use the shortcut technique you can open with windows terminal (wt.exe) instead of wsl.exe with slight modification.

The only issue I can see with either option is if you have multiple linux distributions this may open the wrong one which can be fixed by running wsl.exe --setdefault [your distro] Furthermore, [your distro] can be found with wsl.exe -l

rmoro avatar Apr 21 '23 12:04 rmoro

I tried to create a shortcut according to the instructions. If I run the shortcut in PowerShell, the program opens just fine, though it does not open the file (because the path is not converted and contains backslashes). But if I use "Open With" to open a file (or associate the file), a command window (and perhaps a window with the program?) appears for a fraction of a second and then closes.

rommeswi avatar Jul 07 '23 04:07 rommeswi

for anyone wanting a working answer... you need to convert the path into a linux path so nvim can open it... wsl provides the wslpath command so... a batch file like this will do it. ( use the openwith context option for the filetype you want to open with nvim and point it at this batch file(I named mine OpenWithNVIM.bat)) this will open a new nvim session... you could usr nvr server to open it into an existing one.

@echo off
for /f "usebackq tokens=*" %%i in (`wsl wslpath "%~1"`) do set "FILEPATH=%%i"
echo Input: %1
echo Output: %FILEPATH%
wt.exe -d . wsl nvim "%FILEPATH%"

Dreded avatar Dec 27 '25 02:12 Dreded