Vim with Powershell Vim with Powershell powershell powershell

Vim with Powershell


It is a bit of a hack, but the following works in Vim 7.2. Notice, I am running Powershell within a CMD session.

if has("win32")    set shell=cmd.exe    set shellcmdflag=/c\ powershell.exe\ -NoLogo\ -NoProfile\ -NonInteractive\ -ExecutionPolicy\ RemoteSigned    set shellpipe=|    set shellredir=>endiffunction! Test()  echo system("dir -name")endfunction

Tested with the following...

  • :!dir -name
  • :call Test()


I ran into a similar problem described by many here.

Specifically, calling

:set shell=powershell

manually from within vim would cause powershell to work fine, but as soon as I added:

set shell=powershell

to my vimrc file I would get the error "Unable to open temp file .... "

The problem is that by default when shell is modified, vim automatically sets shellxquote to " which means that shell commands will look like the following:

 powershell -c "cmd > tmpfile"

Where as this command needs to look like this, in order for vim to read the temp file:

 powershell -c "cmd" > tmpfile

Setting shellquote to " in my vimrc file and unsetting shellxquote (i.e. setting it to a blank space) seem to fix all my problems:

set shell=powershellset shellcmdflag=-cset shellquote=\"set shellxquote=

I've also tried taking this further and scripting vim a bit using the system() call:system() with powershell in vim


I suspect that the problem is that Powershell uses the native String encoding for .NET, which is UTF-16 plus a byte-order-mark.

When it's piping objects between commands it's not a problem. It's a total PITA for external programs though.

You can pipe the output through out-file, which does support changing the encoding, but still formats the output for the terminal that it's in by default (arrgh!), so things like "Get-Process" will truncate with ellipses, etc. You can specify the width of the virtual terminal that Out-File uses though.

Not sure how useful this information is, but it does illuminate the problem a bit more.