Using PowerShell as the shell in VIM

226 views
Skip to first unread message

Mike Williams

unread,
Jun 16, 2021, 5:59:50 AM6/16/21
to vim...@googlegroups.com
Hi,

I recently tried to start using Tim Pope's excellent git plugin,
vim-fugitive, on my Windows machine and soon ran into problems. These
were all from having setup Windows PowerShell as my shell to use from
VIM. My settings were ancient and naively cargo-culted from the web
and had worked for my relatively simple purposes. At Tim's prodding I
revisited my shell settings for VIM which improved things but
unfortunately did not manage to get the plugin fully working, so for
now I have to fall back on the ancient cmd.exe shell. Googling around
shows plenty of confusion of how to set up powershell.

And to show my greenness on this I have only just twigged there are
two versions of PowerShell, the one supplied as standard with Windows
(the one I am using, 5.1 aka .NET) and PowerShell Core (6.0) which is
multi-platform which apparently differ in ways I don't know yet. At
least they have different shell names, powershell vs pwsh. For now I
am concentrating on the former since this concerns various Windows
specific issues.

Any road, here is a summary of issues that I encountered trying to
setup PowerShell as the shell.

The powershell command works best without any quotes around it, so
shellquote should be empty (setting shq to a single quote (') makes
the command fail, using double quotes (") causes problems with quoted
arguments). According to the shellquote docs shellquote is set to " on
win32 when shell contains sh. This is not what is happening with
shell=powershell, AFAICT VIM checks for shell ending with sh. Which is
correct, doc or VIM? If the intent is if sh is anywhere then VIM will
need modifying to detect powershell to leave shq unset.

VIM is defaulting shellxquote to " with shell=powershell since that
detects sh within the shell name. Use shell=PowerShell and shellxquote
is not set. AFAICT either setting works but I think it would be less
confusing if sxq is left unset with powershell

Shell command arguments with spaces need to be enclosed in single
quotes, not double quotes. This causes a problem with the
shellescape() function as by default on Windows it uses double quotes.
Unless shellslash is set when it will use single quotes. However this
forces the use of shellslash in a shell that is not unix like. Yes
powershell will accept forward quotes but any paths it reports have
backslashes, so VIM scripts start to get confused when VIM generated
paths and shell generated paths don't match. Yeah scripts can include
file path normalisation functions but I feel they shouldn't have to
(and trying to make Windows file system looks like a unix one is a
never ending job I'd rather not have to do).

shellescape() needs modifying to correctly escape when using
powershell independently of shellslash. Should this be a new shell
setting? Does shellescape() need to be aware when the shell is
powershell and behave differently? Does shellescape() need yet another
argument to handle this in some way I haven't thought of yet?

The shellredir option defaults to just > which loses stderr output.
This can be caught with powershell using redir=>%s\ 2>&1 However
since powershell generates UTF16 this can cause problems for plugins
so the following can be used instead - shellredir=2>&1\ \|\ Out-File\
-encoding\ Default which well transcode to the configured console
codepage. The shellpipe option for powershell also defaults to > which
is not consistent with the doc - it can be set to be shellpipe=>%s\
2>&1 to just capture to a file, or if you want to see the output as
well then use shellpipe=2>&1\ \|\ Tee-Object For both settings VIM
and doc should be updated to be aware of powershell and it's alternate
defaults.

As I said, this is based on my investigations - I may have missed some
things or misunderstood things. As it stands I encounter problems
with scripts using powershell on windows (which is different to pwsh
on unix!) which appear to be fixable with some of the above changes.
Having sane defaults for the various shell settings for powershell
would require less tinkering to get VIM working, and provide a solid
basis for script writers having to cope with different shells.

Any comments or thoughts before I start to dive in?

TTFN
--
Mike

Bram Moolenaar

unread,
Jun 16, 2021, 9:10:34 AM6/16/21
to vim...@googlegroups.com, Mike Williams
It would be very nice if we can have Vim pick the right defaults when
'shell' is "powershell". This at least means using the existing options
and figuring out the best value for each.

If shellescape() needs to be changed, be probably need (yet another)
option. Currently there are "#ifdef MSWIN" lines in the code, so
obviously these need to be changed to use that option. Not sure if we
could re-use an existing option for this.

--
It doesn't really matter what great things you are able to do if
you don't do any of them. (Bram Moolenaar)

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
Reply all
Reply to author
Forward
0 new messages