与Powershell的Vim

Kev*_*dge 54 vim powershell

我在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()
Run Code Online (Sandbox Code Playgroud)

如果我执行此函数(:Test),我会看到无意义的字符(非数字/字母ASCII字符).

如果我使用cmd作为shell,它可以工作(没有-name),所以问题似乎是从powershell输出到vim.

有趣的是,这很有用:

:!dir -name
Run Code Online (Sandbox Code Playgroud)

就像这样:

:r !dir -name
Run Code Online (Sandbox Code Playgroud)

更新: 确认大卫提到的行为

如果在_vimrc中执行上面提到的set命令,:Test输出无意义.但是,如果直接在vim而不是_vimrc中执行它们,则:Test按预期工作.

此外,我尝试使用iconv,以防它是一个编码问题:

:echo iconv( system("dir -name"), "unicode", &enc )
Run Code Online (Sandbox Code Playgroud)

但这没有任何区别.我可能会使用错误的编码类型.

有谁知道如何使这项工作?

Nat*_*ley 25

这有点像黑客,但以下在Vim 7.2中有效.请注意,我在CMD会话中运行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
Run Code Online (Sandbox Code Playgroud)

经过以下测试......

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


act*_*ctf 15

我遇到了许多人在这里描述的类似问题.

特别是,打电话

:set shell=powershell
Run Code Online (Sandbox Code Playgroud)

从vim内部手动将导致PowerShell正常工作,但是一旦我添加:

set shell=powershell
Run Code Online (Sandbox Code Playgroud)

到我的vimrc文件,我会收到错误"无法打开临时文件...."

问题是默认情况下修改shell时,vim会自动将shellxquote设置为"这意味着shell命令将如下所示:

 powershell -c "cmd > tmpfile"
Run Code Online (Sandbox Code Playgroud)

这个命令需要看起来像这样,为了让vim读取临时文件:

 powershell -c "cmd" > tmpfile
Run Code Online (Sandbox Code Playgroud)

将shellquote设置为"在我的vimrc文件中并取消设置shellxquote(即将其设置为空格)似乎解决了我所有的问题:

set shell=powershell
set shellcmdflag=-c
set shellquote=\"
set shellxquote=
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用system()调用进一步编写vim: 在vim中使用powershell的system()