我在 Windows 上使用 gvim。
在我的 _vimrc 中我添加了:
set shell=powershell.exe
set shellcmdflag=-c
set shellpipe=>
set shellredir=>
function! Test()
echo system("dir -name")
endfunction
command! -nargs=0 Test :call Test()
如果我执行此函数 (:Test),我会看到无意义字符(非数字/字母 ASCII 字符)。
如果我使用 cmd 作为 shell,它可以工作(没有 -name),所以问题似乎在于将 powershell 的输出获取到 vim。
有趣的是,这效果很好:
:!dir -name
就像这样:
:r !dir -name
更新:确认 David 提到的行为
如果在 _vimrc 中执行上面提到的 set 命令,:Test 会输出无意义的内容。但是,如果您直接在 vim 中而不是在 _vimrc 中执行它们,则 :Test 将按预期工作。
另外,我尝试使用 iconv 以防出现编码问题:
:echo iconv( system("dir -name"), "unicode", &enc )
但这没有任何区别。不过,我可能使用了错误的编码类型。
有人知道如何实现这个功能吗?
最佳答案
这有点 hack,但以下内容在 Vim 7.2 中有效。请注意,我在 CMD session 中运行 Powershell。
if has("win32")
set shell=cmd.exe
set shellcmdflag=/c\ powershell.exe\ -NoLogo\ -NoProfile\ -NonInteractive\ -ExecutionPolicy\ RemoteSigned
set shellpipe=|
set shellredir=>
endif
function! Test()
echo system("dir -name")
endfunction
使用以下内容进行测试...
:!dir -name
:调用 Test()
关于Vim 与 Powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/94382/