Answers:
最好的方法是使用has(),使用此功能,您可以检查Vim的功能;来自:help feature-list以下操作系统的特定功能
:
macunix Macintosh version of Vim, using Unix files (OS-X).
unix Unix version of Vim.
win32 Win32 version of Vim (MS-Windows 95 and later, 32 or
64 bits)
win32unix Win32 version of Vim, using Unix files (Cygwin)
和一些较旧的(半不推荐使用的)系统:
amiga Amiga version of Vim.
os2 OS/2 version of Vim.
win16 Win16 version of Vim (MS-Windows 3.1).
win64 Win64 version of Vim (MS-Windows 64 bit).
win95 Win32 version for MS-Windows 95/98/ME.
例:
if has('win32')
echo "Someone please open the Window(s)!"
endif
另一种具有更大灵活性的方法是调用external uname,这还允许您获取版本号,例如:
let uname = system('uname -a')
请注意,uname在大多数Windows系统上都不存在。
通常最好使用功能检测,而不是OS检测。例如,通过使用中的功能之一has()或检查是否存在某些路径。200_success的帖子对此进行了很好的概述,因此在此不再重复相同的内容。
:h feature-list
has()是个好主意,直到声音你试试在Mac OS X:在默认情况下/usr/bin/vim,has('unix')是真实的,但两者has('macunix')并has('mac')是假的,同时,在常规的MacVim下载,三者都是真实的你是否使用GUI或TUI。
因此,最好的解决方案是将has('winXX')Windows和uname类似Unix的系统混合使用。请注意,unameend 的输出以换行符结尾,因此在使用前必须将其清除。
这是我使用了一段时间的代码,已更新为win64:
if !exists("g:os")
if has("win64") || has("win32") || has("win16")
let g:os = "Windows"
else
let g:os = substitute(system('uname'), '\n', '', '')
endif
endif
之后,您可以在g:os任何位置使用变量vimrc:
if has("gui_running")
if g:os == "Darwin"
set guifont=Fira\ Mono:h12
elseif g:os == "Linux"
set guifont=Fira\ Mono\ 10
elseif g:os == "Windows"
set guifont=Fira_Mono:h12:cANSI
endif
endif
has('unix')准确。奇怪的has('macunix')是,这是错误的。这听起来像是个错误?
has('macunix')似乎依赖MACOS_X_UNIX,这是一套,如果uname是Darwin在configure脚本...
has ("win32")即使在64位Vim中,它似乎也对我有用。
has('unix')在Windows Git Bash上也是如此,因此对于将真实的unix与仿真区分开来不是很有用。
环境变量将很有用。
要检测tmux,可以检查if !empty($TMUX)或if $TERM == 'screen'。
您还可以从环境变量(如$MACHTYPE(由Bash设置)或)推断出操作系统$PATH。
要检测是否安装了诸如TFS之类的executable()功能,可以使用该功能。
$MACHTYPE。您将必须先执行类似操作let machtype=system('echo -n $MACHTYPE')。
正如其他人已经指出的那样,可靠的检测可能很棘手。最好不要重新发明轮子,所以我想提到提供和功能的vim-misc库。xolox#misc#os#is_mac()xolox#misc#os#is_win()
我尝试了has('unix'),但是由于所有的Linux,Mac和Windows Git Bash都返回true了它,因此我发现它没有帮助。
我发现uname它适用于Linux,Mac,Windows,并且没有太多粒度。
let uname = substitute(system('uname'), '\n', '', '')
" Example values: Linux, Darwin, MINGW64_NT-10.0, MINGW32_NT-6.1
if uname == 'Linux' || uname == 'Darwin'
" do linux/mac command
else " windows
" do windows command
endif