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

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

问题描述:

我想做一个函数来执行以下操作

I would like to make a function which does the following

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

有谁知道如何判断 vim 是从哪个程序运行的?

Does anyone know how to tell what program vim is being run from?

echo &term 在两种情况下都输出win32".

echo &term outputs "win32" in both cases.

据我所知,你不需要检查 vim 是从什么运行的.您需要检查 &shell 的值,因为如果 &shell 选项告诉它在 powershell 中运行命令,vim 将在 powershell 中运行命令.如果 shell 的名称是 posh 然后检查可以用

From what I see you don’t need to check what vim was run from. You need to check the value of &shell because vim will run command in powershell if &shell option tells it to run command in powershell. If name of the shell is posh then checking may be done with

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

.但我宁愿建议直接使用类似

. But I would rather suggest to run powershell directly with something like

call system('posh -c '.shellescape('only works in powershell'))

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

(note: I am not sure about what 'posh -c ' string should actually look like) or temporary set &shell option:

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 中运行.

(again, not sure about exact values of options): these ways command will always run in powershell.