3

I am updating my PowerShell aliases to include utilities from the Windows Subsystem for Linux. For example, I want to launch vim from PowerShell.

Set-Alias -name 'vim' -Value 'Start-Vim'

function Start-Vim {
    wsl vim $args
}

This Alias works great if I pass in a simple filename (e.g., vim note.txt), but it fails if I pass in an absolute file path, a relative path anchored with .\ or a relative path with no anchor.

I tried using Resolve-Path to at least get to a single place (a fully resolved path), but that still doesn't work. It launches vim, but with that path as a new file ("C:\temp\APIM Swap.ps1" [New File] in status line).

I started down the path of replacing strings (replace \ with /), but that only solves relative path with no anchor, and string manipulation doesn't feel like the right solution. Plus, drive letters map to mnts in linux, so I need to deal with that.

Is there a way to convert a file path in Windows to its file path in Windows Subsystem for Linux? Or is there another way to integrate WSL utilities?

chicks
  • 3,639
  • 10
  • 26
  • 36
Erick T
  • 191
  • 1
  • 4
  • 1
    I would look at `Resolve-Path` to address the issue of relative paths. – EBGreen May 09 '18 at 18:08
  • 1
    I tried that approach, but the resulting resolved path still doesn't appear to be a valid WSL path – Erick T May 09 '18 at 18:10
  • 1
    Please update your question with what you tried and in what fashion it did not work. – EBGreen May 09 '18 at 18:11
  • AFAIK for it to work properly you must be using relative paths in 1709. Though newer releases (maybe 1803) might have a utility `wslpath` that converts things which you could call in your function/script. https://github.com/MicrosoftDocs/WSL/releases/tag/17046 – Zoredache May 10 '18 at 00:41
  • wslpath seems like exactly what I need. I don't have it in my version yet, but will use that when it comes out – Erick T May 10 '18 at 19:43

1 Answers1

2

I am able to use the following using Windows 10 Home 177763 and Alpine WSL.

Set-Alias -name 'vi' -Value 'Start-Vi'
function Start-Vi {
    wsl vi (Resolve-Path -Relative $args)
}

Note that this will fall over if you don't provide an argument for the function, or if your path is deeper than then working directory.

* UPDATE *

Okay so I went and had a look at wslpath, which is a linux command patched into wsl. Now you can do this -

Set-Alias -name 'wslpath' -Value 'Get-WSLPath'
function Get-WSLPath {
    $strlit='"'
    wsl wslpath $strlit$args$strlit
}
Set-Alias -name 'nano' -Value 'Start-Nano'
function Start-Nano {
    wsl nano $args
}

which allows you to run the following piece of ugly code

nano (wslpath .\.ssh\known_hosts)  

which now works for any relative path, but bombs if there are no arguments.

* UPDATE AGAIN *

And of course most of that is totally unnecessary because you can just use :

Set-Alias -name 'vi' -Value 'Start-Vi'
function Start-Vi {
    wsl vi (wsl wslpath "'$args'")
}