Linux – Open file behavior using Ranger in Windows Subsystem for Linux

rangerwindows-subsystem-for-linux

I am using Microsoft Windows ([Version 10.0.15063]) subsystem for Linux and observe an unexpected behavior when opening files using Ranger file manager in bash.

For example, I have ~/.config/rifle.conf set to open .txt extensions in Sublime Text 3:

ext txt = "/mnt/c/Program Files/Sublime Text 3/sublime_text.exe" "$@"

When I hit enter to open an existing text file that contains data, Sublime does open the file, but there is no actual text/data in the file displayed in Sublime. I can close the file and open using Windows file explorer and the text is there.

Interestingly, the path Sublime sees is 'C\mnt\c\path\to\my\file.txt'.

Is this some issue with Windows not being able to see /mnt? I'd love to be able to use Ranger as my file manager on WSL, has anyone seen this behavior and possibly discovered a fix? Thanks all!

Best Answer

As laktak explained, WSL will not translate the file path from Unix pathing to Windows pathing. I just wrote a Gist on how I handle this, reproduced below:

Make sure you have WSL with the Windows 10 Fall Creators Update installed. Ranger uses rifle as a file handler and you need its config file, rifle.conf. If you dont have it (should be in ~/.config/ranger/rifle.conf), run the command ranger --copy-config=rifle, then edit the resulting file.

To run Windows applications from Ranger, we will use cmd.exe /C start "" <file>, which works after the Fall Creators Update. We will solve the pathing issue by using sed to translate the path.

Add below code to your rifle.conf and you will be able to run Windows applications for the chosen file extensions.

ext docx?|xlsx?|pptx?|pdf = echo "$@" | sed -e 's;/mnt/\(.\);\1:;' -e 's/.*/"&"/' -e 's:/:\\:g' | xargs cmd.exe /C start ""

start should be able to run applications associated with file extensions implicitly, but if for some reason it doesnt work, you can also explicitly tell start which executable to run. Just remove the "" and add one of excel, winword, powerpnt, AcroRd32.exe, etc. Note that in this case you will need one line in rifle.conf for each application. As an example:

ext docx? = echo "$@" | sed -e 's;/mnt/\(.\);\1:;' -e 's/.*/"&"/' -e 's:/:\\:g' | xargs cmd.exe /C start winword
ext xlsx? = echo "$@" | sed -e 's;/mnt/\(.\);\1:;' -e 's/.*/"&"/' -e 's:/:\\:g' | xargs cmd.exe /C start excel

Additional Reading

Related Question