Vim for remote Windows

Our development team is constantly improving the RUDDER software. They have to be at the cutting edge of technology. So they give you tips and feedbacks on the codes and techniques they use when they discover something interesting.
Here, Felix explains how you can use SSH and Vim to edit Powershell script directly in Windows systems.

Since Windows 10, Windows is shipped with an optional built-in OpenSSH server.

This is a great news, since I am used to dev on my Linux environment and with this built-in SSH server, I should be able to keep my daily tools (a shell and the Vim editor) to get the work done on my servers.

Configuring the SSH access was pretty easy, by default the password and key authentification are enabled, creating a .ssh/authorized_keys under my user home on the Windows machine was enough for my needs.

Next step, be able to run Powershell commands and edit remote files.

Powershell as default SSH shell

The SSH server runs cmd.exe as default shell, it can be changed via a registry entry.

# To get the powershell default path

(Get-Command -name powershell.exe).Path

# To set it as default SSH shell

Set-Itemproperty -path 'HKLM:\SOFTWARE\OpenSSH' -Name 'DefaultShell' -value (Get-Command -name powershell.exe).Path
 

Remote file edition using vim

Vim comes with a feature to edit remote file using SCP. The syntax is described there.

But the issue here is that Windows and Linux are using different path syntaxes and escaping methods.

Below are the different syntaxes that I ended up with when trying to access my remote files.

From Windows to Linux:
# Copy "C:\my_folder\my_file" to the local folder

scp -P2209 administrator@127.0.0.1:/my_folder/my_file .

  

# Copy "C:\Program Files/Rudder/policy/rudder.ps1" to the local folder

# The space make it quite hard, I could not find any method without the -T option

scp -T -P2209 administrator@127.0.0.1:\"/Program\ Files/Rudder/policy/rudder.ps1\" .
 
From Linux to Windows:
# Copy my_file to "C:\my_folder\my_file"

scp -P2209 my_file administrator@127.0.0.1:"C:/my_folder/"

# Copy my_file to "C:\Program Files\Rudder\"

scp -P2209 my_file 'administrator@127.0.0.1:"C:/Program Files/Rudder/"' 
And now if I want to edit remote files:
# Trivial if there is no spaces

vim scp://administrator@127.0.0.1:2209//my_folder/my_file 
But I was unable to find a syntax working for path using spaces, I tried a bunch of different potential syntaxes, none were working. As an ugly workaround, I ended up defining a Joined folder to link the Rudder folder to one without any space in its path.
# In cmd.exe to create the link

mklink /J "Rudder" "C:\Program Files\Rudder"

  

# I can now access my files using

vim scp://administrator@127.0.0.1:2209//Rudder/policy/rudder.ps1 

There are most likely better ways to achieve this but still, I was quite happy to not have to use anymore remote desktop UI tools nor shared folders to work on my Windows dev machines.

If you have anything better, feel free to send me an email at fda@rudder.io

Félix Dallidet

Share this post

Share on facebook
Share on twitter
Share on linkedin
Share on print
Share on email
Scroll to Top