Vim 与 Powershell

标签 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()

如果我执行此函数 (: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/

相关文章:

vim - 将当前行保存在变量中

vim - vim 中的多个自动命令

macos - 在 Mac OS X 上编译带有 +clientserver 功能的 Vim 7.3

windows-7 - powershell 作为 gvim(vim) :shell

windows - 在任务计划程序中运行 .ps1 脚本时如何隐藏 PowerShell 窗口?

vim - 在 vim 中按字母顺序排列给定范围的行

powershell - 寻求对 Powershell ExpandProperty 行为的解释

Powershell:当命令返回错误时如何删除堆栈跟踪?

PowerShell 过滤文件大小

.net - Powershell HTTPS REST api GET/POST