Skip to content

Instantly share code, notes, and snippets.

@TaipanRex
Last active November 16, 2023 10:05
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TaipanRex/5db9a19304743fe6367b9ef291d2b69b to your computer and use it in GitHub Desktop.
Save TaipanRex/5db9a19304743fe6367b9ef291d2b69b to your computer and use it in GitHub Desktop.
How to launch Windows applications from Ranger in Windows Subsystem for Linux (WSL)

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. The problem is that Ranger will feed in file paths using Unix pathing, but start expects Windows pathing. We will solve this 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

Update: There is a command wslpath that does path translation between Windows/Unix and Unix/Windows. This was added in the 1704 release of WSL. There are no man pages for wslpath, but the release notes explain the command and its options.

Our code can therefore be simplified:

ext docx?|xlsx?|pptx?|pdf = wslpath -w "$@" | sed -e 's/.*/"&"/' | xargs cmd.exe /C start ""

Additional Reading

@Migacz85
Copy link

Migacz85 commented Jul 9, 2021

Ok - so for yanking paths:

  1. open rc.conf
  2. find 'map yp' and paste something like:
    map yp shell echo "%S" | clip.exe
    map yP shell wslpath --w "%S" | clip.exe

first will copy unix path second windows path. And we are gucci :-)

@nxtexploit
Copy link

Hi,

  1. Create a file rifle.conf in ~/.config/ranger/
  2. Open that file sudo nano ~/.config/ranger/rifle.conf
  3. And add this below code in rifle.conf :
ext ods?|xls?|xlsx?|csv?|ics?|vcf?|3dm?|3ds?|max?|bmp?|dds?|gif?|jpg?|jpeg?|png?|psd?|xcf?|tga?|thm?|tif?|tiff?|yuv?|ai?|eps?|ps?|svg?|dwg?|dxf?|gpx?|kml?|kmz?|webp?|3g2?|3gp?|aaf?|asf?|avchd?|avi?|drc?|flv?|m2v?|m4p?|m4v?|mkv?|mng?|mov?|mp2?|mp4?|mpe?|mpeg?|mpg?|mpv?|mxf?|nsv?|ogg?|ogv?|ogm?|qt?|rm?|rmvb?|roq?|srt?|svi?|vob?|webm?|wmv?|yuv?|aac?|aiff?|ape?|au?|flac?|gsm?|it?|m3u?|m4a?|mid?|mod?|mp3?|mpa?|pls?|ra?|s3m?|sid?|wav?|wma?|xm?|7z?|a?|apk?|ar?|bz2?|cab?|cpio?|deb?|dmg?|egg?|gz?|iso?|jar?|lha?|mar?|pea?|rar?|rpm?|s7z?|shar?|tar?|tbz2?|tgz?|tlz?|war?|whl?|xpi?|zip?|zipx?|xz?|pak?|exe?|msi?|bin?|command?|sh?|bat?|crx?|c?|cc?|class?|clj?|cpp?|cs?|cxx?|el?|go?|h?|java?|lua?|m?|m4?|php?|pl?|po?|py?|rb?|rs?|sh?|swift?|vb?|vcxproj?|xcodeproj?|xml?|diff?|patch?|html?|js?|html?|htm?|css?|js?|jsx?|less?|scss?|wasm?|php?|ppt?|odp?|doc?|docx?|ebook?|log?|md?|msg?|odt?|org?|pages?|pdf?|rtf?|rst?|tex?|txt?|wpd?|wps?|mobi?|epub?|azw1?|azw3?|azw4?|azw6?|azw?|cbr?|cbz?| = wslpath -w "$@" | sed -e 's/.*/"&"/' | xargs cmd.exe /C start ""
  1. And save it and you're done.
  2. Now you can open any file from ranger with your WSL :)

regards,
@nxtexploit

@Lvjinhong
Copy link

WSL2可以吗? 我在WSL中使用为什么会出现 Command not found: `ext'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment