Reputation: 201
I'm having trouble writing a program that takes the /path/to/file
in an WSL Ubuntu environment and opens that file in its default Windows program. The biggest issue here is converting the /path/to/file/for/WSL
into a path\to\file\for\windows
. This is what I wrote and put in my .bashrc
file:
# Function to get open-wsl to work
function open-from-wsl() {
echo "opening"
cmd_directory = echo "$1" | sed 's/^\///' | sed 's/\//\\/g' | sed 's/^...../\0C:/' | sed 's/^\mnt//'
cmd_directory = echo "$cmd_directory" | sed 's/^..//'
cmd.exe /C start $cmd_directory
}
Ideally what this is supposed to accomplish is if I type :
open-from-wsl /mnt/c/Users/DavidG/Google\ Drive/folder/file.PDF
I'll have my file.PDF
open in my default PDF viewer. As of right now however, I get the error cmd_directory: command not found
and then my command prompt window opens up. This of course is being written so that I can open any file from WSL and have it open in it's default program, it doesn't just pertain to PDFs.
EDIT: I've adjusted the code like so thanks to Socowi's input:
# Function to get open-wsl to work
function open-from-wsl() {
echo "opening"
cmd_directory=$(echo "$1" | sed 's#\##')
cmd_directory=$(echo "$cmd_directory" | sed 's/^\///' | sed 's/\//\\/g' | sed 's/^...../\0C:/' | sed 's/^\mnt//')
cmd_directory=$(echo "$cmd_directory" | sed 's/^..//')
cmd.exe /C start $cmd_directory
}
My issue now seems to be that I can't remove the \
from /mnt/c/Users/DavidG/Google\ Drive/folder/file.PDF
, which was my goal by adding the line
cmd_directory=$(echo "$1" | sed 's#\##')
Upvotes: 0
Views: 492
Reputation: 6453
A much simpler solution is to create a file in /usr/bin/ called v (v for view, i like short commands, but you can call it anything you like) containing the following:
cmd.exe /C start $(wslpath -w "$(pwd)/$1")
Now assuming /usr/bin is in the wsl path just call v foo.txt
to open a file in its default windows editor.
wslpath is used to convert the wsl path to a dos path. This is now included in the wsl install (link)
Upvotes: 1
Reputation: 201
After searching among various questions pertaining to adjacent-esque issues. I finally found the solution. This is the function that I ended up putting into my .bashrc
file:
# Function to get open an arbitrary file in its default Windows program
function open-from-wsl() {
echo "opening"
cmd_directory=$(echo "$1" | sed 's#[\]##')
cmd_directory=$(echo "$cmd_directory" | sed 's/^\///' | sed 's/\//\\/g' | sed 's/^...../\0C:/' | sed 's/^\mnt//')
cmd_directory=$(echo "$cmd_directory" | sed 's/^..//')
echo "$cmd_directory"
cmd.exe /C start "" "$cmd_directory"
}
This will allow you to open any file in your computer (assuming you know the WSL path to it) in it's default Windows program.
Upvotes: 0