Vimscript:如何获得运行Vim的操作系统?

时间:2011-05-27 00:40:35

标签: vim

我有vim插件可以在不同的机器上运行,有时需要根据它是Windows,Linux,Mac来做不同的事情。

测试操作系统最简单的方法是什么?我知道我可以解析:version命令的输出。是否有更简单的东西可以揭示操作系统?

3 个答案:

答案 0 :(得分:14)

来自谷歌: 您可以使用has():help feature-list下的功能列表 确定什么类型的Vim(以及因此运行的OS)。

if has('win32')
   ... win32 specific stuff ...
endif

从功能列表帮助主题中搜索“Vim版本” 应该带您到可以检查的不同版本。

答案 1 :(得分:8)

除了@ John的答案之外,这里还列出了可能的操作系统:

"▶2 os.fullname
for s:os.fullname in ["unix", "win16", "win32", "win64", "win32unix", "win95",
            \         "mac", "macunix", "amiga", "os2", "qnx", "beos", "vms"]
    if has(s:os.fullname)
        break
    endif
    let s:os.fullname='unknown'
endfor
"▶2 os.name
if s:os.fullname[-3:] is 'nix' || s:os.fullname[:2] is 'mac' ||
            \s:os.fullname is 'qnx' || s:os.fullname is 'vms'
    let s:os.name='posix'
elseif s:os.fullname[:2] is 'win'
    let s:os.name='nt'
elseif s:os.fullname is 'os2'
    let s:os.name='os2'
else
    let s:os.name='other'
endif
"▲2

这是我的frawor插件用于确定当前操作系统的代码。

答案 2 :(得分:0)

由于我对Python的热爱:

python << endpython
import sys
sys.platform
endpython

如果需要,也可以os.name