0

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#\##')
2
  • Remove the spaces around = and encapsulate echo ... | sed ... | ... in $(). Commented Mar 14, 2019 at 17:37
  • I was under the impression that wslpath only took Windows paths and converted them to a Ubuntu path Commented Mar 14, 2019 at 21:31

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.