I'm trying to write a simple function in Vim to return the results of a powershell command. I keep getting gibberish in the results though.
I think this may be an encoding problem, but as you'll see the problem is strange since it "sort of works". I don't have any solution to the problem though.
With the following non-default shell options set in vim:
set shell=powershell
set shellcmdflag=-c
Given the following function:
function! Test()
let result = system("ls")
call setline(1, result)
endfunction
When I run (from C:\Windows):
:call Test()
The following is written to my buffer:
^@^@ Directory: C:\Windows^@^@^@ Mode LastWriteTime Length Name ^@-------
....continues
However when I run the following command:
:r!ls
I get back exactly what I would expect (i.e. the powershell results of ls) Even more interesting is when I run the command:
:echo system("ls")
The results look correct
I've tried modifying my original function as follows:
function! Test()
let result = system("ls")
echo result
call setline(1, result)
endfunction
and the value echo'ed out is exactly what I would expect - yet I still see gibberish
I've also tried the following modification to my function:
function! Test()
let result = system("ls")
let conv = iconv(result, "utf-8", &enc)
call setline(1, conv)
endfunction
But the results are exactly the same (i.e. they include the ^@^@ symbols and other gibberish)
My guess as to what's happening is that powershell cmds which are redirected using > produce utf-16 output, and vim is unable to deal with this. I get the following from powershell (file in this case is the gnu32 program):
PS> ls > test
PS> file test
test; Little-endian UTF-16 Unicode text, with CRLF, CR line terminator
I've also tried playing around with $OutputEncoding, without any success, as described here: http://blogs.msdn.com/b/powershell/archive/2006/12/11/outputencoding-to-the-rescue.aspx
Anyone have any ideas what I'm doing wrong here?