3

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!

2

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

| improve this answer | |
0

WSL doesn't handle absolute path translation AFAIK

You can either

  • translate the path yourself (for example by using a bash function to replace /mnt/c/ with c:\)
  • or use relative paths
| improve this answer | |
  • I will test the first suggestion as soon as time allows and accept answer if this is a viable work around. For now, thank you for taking a look. – gpanda Nov 6 '17 at 23:37
0

For translating the Linux path /mnt/c/Users/myusername/Desktop
into a windows path C:\Users\myusername\Desktop
three steps have to be performed:

Step 1: Append double-quotes at the beginning and the end of the path (essential for whitespaces):

sed -e 's;.*;"&";'
#   s … substitute
#   ; … delimiter character (can by any character)
#   .* … any character `.` occurring any number of times `*` will be replaced by:
#   " … at the beginning and " at the end.

Step 2: Substitute /mnt/c/ by C::

sed -e 's;/mnt/c/;C:;'
#   s … substitute field 1 `/mnt/c/` by field 2 `C:`

Step 3: Substitute all slashes / by back-slashes \:

sed -e 's;/;\\;g'
#   s … substitute field 1 `/` by field 2 `\`
#   Note that there is a second `\` for escaping.

Testing all three part in a Linux terminal:

echo '/mnt/c/Users/myusername/Desktop' | sed -e 's;.*;"&";' -e 's;/mnt/c/;C:;' -e 's;/;\\;g'
"C:Users\myusername\Desktop"

Ranger uses rifle as file opener. On my native Linux distribution my rifle.conf only contains one line for launching all file types with its default application:

else X, flag f = xdg-open "$@"

In case of the WSL this would look like the following:

cat << EOT >> ~/.config/ranger/rifle.conf
else X, flag f = echo "\$@" | sed -e 's;.*;"&";' -e 's;/mnt/c/;C:;' -e 's;/;\\\;g' | xargs cmd.exe /C start ""
EOT
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.