Vimscript: how do I get the OS that is running Vim?
I've got vim plugin that runs on different machines and sometimes needs to do things differently depending on whether it's Windows, Linux, Mac.
What's easiest way to test for the operating system? I know I could parse the output of :version command. Is there something simpler that will re开发者_运维技巧veal the OS?
From google:
You can use has()
and the list of features under :help feature-list
to
determine what type of Vim (and therefore under which OS is running).
if has('win32')
... win32 specific stuff ...
endif
Search for "version of Vim" from the feature-list help topic and that should bring you to the different versions for which you can check.
In addition to @John's answer here is a full list of possible operating systems:
"▶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
This is the code used by my frawor plugin for determining current operating system.
Due to my love of Python:
python << endpython
import sys
sys.platform
endpython
Also maybe os.name
if needed.
精彩评论