如何判断vim是否在命令行和powershell中运行

时间:2012-08-11 15:26:21

标签: windows powershell vim

我想创建一个执行以下操作的函数

if (vim is running in powershell)
    call system("only works in powershell")
else
    echo "Skipping powershell command"

有谁知道如何判断vim的运行程序是什么?

编辑:echo &term在两种情况下输出“win32”。

2 个答案:

答案 0 :(得分:6)

从我看到你不需要检查运行什么vim。您需要检查&shell的值,因为如果&shell选项告诉它在powershell中运行命令,vim将在powershell中运行命令。如果shell的名称是posh,则可以使用

进行检查
if stridx(&shell, 'posh')!=-1
    call system('only works in powershell')
else
    echo 'Skipping powershell command'
endif

。但我宁愿建议直接用类似

的方式运行powershell
call system('posh -c '.shellescape('only works in powershell'))

(注意:我不确定'posh -c '字符串应该是什么样子)或临时设置&shell选项:

let savedsh=[&shell, &shellcmdflag, &shellxquote, &shellxescape, &shellquote, &shellpipe, &shellredir]
set shell=posh shellcmdflag=-c shellxquote=\" shellquote=\" shellpipe=> shellredir=>
try
    call system('only works in powershell')
finally
    let [&shell, &shellcmdflag, &shellxquote, &shellxescape, &shellquote, &shellpipe, &shellredir]=savedsh
endtry

(同样,不确定选项的确切值):这些方法命令将始终在powershell中运行。

答案 1 :(得分:3)

您可以查看$term。例如,在我的cygwingit bash上,我$termcygwin

对于Powershell,您可以在您的个人资料中设置以下内容并进行检查:

$env:TERM = "posh"

请注意,对于cmd和powershell,&shell将为cmd.exe。