Monday, January 10, 2011

Minigotcha: escaping path strings for cmd.exe

There's a very handy plugin for (g)Vim called netrw, which provides the capability to edit files over ssh/scp/etc. To set it up with Putty on Windows, the suggested lines to add to $MYVIMRC were:
let g:netrw_cygwin = 0
let g:netrw_scp_cmd = "\"C:\\Program Files\\PuTTY\\pscp.exe\" -pw mypasswd "

I'm not sure why this worked for them and not me, but perhaps I'm using an older (or newer) version of netrw, or the cmd.exe behaves differently in Vista. In any case, it didn't work and gave the infuriating output of:
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

This is annoying since the path for pscp is obviously escaped. However, it turns out that cmd.exe follows a slightly strange, arbitrary protocol for escaping space pathstrings - it uses another " character (ack, ugly and confusing... why? The argument is already in quotes...):
C:\Windows\system32\cmd.exe /c "C:\Program" Files\PuTTY\pscp.exe"

PuTTY Secure Copy client
Release 0.60

Okay then! So this works:
let g:netrw_scp_cmd ="\"C:\\Program\" Files\\PuTTY\\pscp.exe\""

No comments:

Post a Comment