WinSCP Call WSL Application as Editor

Advertisement

Kacy_Stocks
Joined:
Posts:
7
Location:
Washington, Utah

WinSCP Call WSL Application as Editor

I've been trying for a few hours to figure out how to use Neovim (for example) from within WSL via the external editor features in WinSCP.
Does anyone have a method to get this working?
So far I've tried:
powershell.exe -ExecutionPolicy Bypass "C:\tools\ps_wsl_nvim.ps1" !.!
With the script:
& "wsl.exe nvim --server localhost:6666 --remote"
I've also tried:
wsl.exe nvim --server localhost:6666 --remote !.!
and
<wslpath>wsl.exe nvim --server localhost:6666 --remote !.!
and
powershell.exe -ExecutionPolicy Bypass "wsl.exe nvim --server localhost:6666 --remote" /mnt/c/!.!
etc...
Last edited by Kacy_Stocks on 2024-10-17 23:03; edited 1 time in total

Reply with quote

Advertisement

Kacy_Stocks
Joined:
Posts:
7
Location:
Washington, Utah

How wsl calls from powershell work

By running the following command in PS – it opens the file on WSL's Neovim:
wsl nvim --server localhost:6666 --remote <file>

Reply with quote

Kacy_Stocks
Joined:
Posts:
7
Location:
Washington, Utah

Update

First I had to install PowerShell 7 for this to work.

Turn on Neovim's headless server:
nvim -n --headless --listen localhost:6666 &
For the Window's side, I used Neovide to house Nvim:
neovide.exe --server localhost:6666
Install from releases on GitHub somewhere on your Windows Path:
https://github.com/neovide/neovide

In WinSCP's External Editor:
pwsh.exe -ExecutionPolicy Bypass -File "C:\tools\ps_wsl_nvim.ps1" !.!
Here's the script I pieced together:
Don't use this!!!! See the script in "Got it"
Set-Variable "nvim_path=/usr/local/bin/nvim"
Set-Variable "file=%~1"
 
# rem Replace backslashes with forward slashes and remove quotes
Set-Variable "file=%file:\=/%"
Set-Variable "file=%file:\=%"
 
# rem Extract the drive letter and the path
Set-Variable "drive=%file:~0,1%"
Set-Variable "path=%file:~2%"
 
# rem Combine them into the WSL path format
Set-Variable "wsl_path=/mnt/c%path%"
 
C:\Windows\System32\wsl.exe -u root sh -c "/usr/local/bin/nvim --server localhost:6666 --remote \"%wsl_path%\""
Saved this at C:\tools\ps_wsl_nvim.ps1

I opens up a new buffer in WSL's neovim so far – how do I get it to open up the expected file?
Last edited by Kacy_Stocks on 2024-11-07 21:38; edited 3 times in total

Reply with quote

Kacy_Stocks
Joined:
Posts:
7
Location:
Washington, Utah

Got it

See the above steps – but replace the script used above with this:
$file=$args[0]
$file=$file.replace('\', '/')
$file=$file.replace('"', '')
$file=$file.replace('C:', '/mnt/c')
 
C:\Windows\System32\wsl.exe -u root sh -c "/usr/local/bin/nvim --server localhost:6666 --remote $file"
You can also use Python:
import subprocess
import sys

file = sys.argv[1]
print(f"\nFile on Windows: {file}")
file = file.replace('\\', '/')
file = file.replace('"', '')
file = file.replace('C:', '/mnt/c')
print(f"\nFile from WSL: {file}\n")

subprocess.check_output(f"C:\\Windows\\System32\\wsl.exe -u root sh -c \"/usr/local/bin/nvim --server localhost:6666 --remote {file}\"")
With the WinSCP editor:
python.exe "C:\tools\wsl_nvim.py" !.!
Last edited by Kacy_Stocks on 2024-11-07 21:40; edited 1 time in total

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum