如何在我的 .vimrc 文件中检测 OS X,因此某些配置仅适用于 OS X? [英] how do I detect OS X in my .vimrc file, so certain configurations will only apply to OS X?
问题描述
我在我的笔记本电脑 (OS X) 和几台服务器 (Solaris & Linux) 上使用我的 .vimrc 文件,并且假设有一天可以在 Windows 机器上使用它.我知道如何检测 unix 和 windows,但如何检测 OS X?(就此而言,有没有办法区分 Linux 和 Solaris 等.是否有一个包含has"可以接受的所有字符串的列表?我的 Google-fu 什么也没找到.)
I use my .vimrc file on my laptop (OS X) and several servers (Solaris & Linux), and could hypothetically someday use it on a Windows box. I know how to detect unix generally, and windows, but how do I detect OS X? (And for that matter, is there a way to distinguish between Linux and Solaris, etc. And is there a list somewhere of all the strings that 'has' can take? My Google-fu turned up nothing.)
例如,我会使用这样的东西:
For instance, I'd use something like this:
if has("mac")
" open a file in TextMate from vi: "
nmap mate :w<CR>:!mate %<CR>
elseif has("unix")
" do stuff under linux and "
elseif has("win32")
" do stuff under windows "
endif
但显然mac"不是正确的字符串,我尝试过的任何其他字符串也不是.
But clearly "mac" is not the right string, nor are any of the others I tried.
更新:下面的答案(macunix")似乎很明显应该工作,但由于某种原因它没有.(也许 Apple 没有正确编译 vim 来回应这个问题?似乎不太可能.)
UPDATE: The answer below ("macunix") seems fairly clearly like it should work, but for some reason it doesn't. (Perhaps Apple didn't compile vim properly to respond to this? Seems unlikely.)
无论如何,我想我需要转移问题的焦点:有没有人有可以达到相同目的的解决方案?(也就是说,成功检测到 .vimrc 文件正在 Mac OS X 上使用.)
At any rate I guess I need to shift the focus of the question: does anyone have a solution that will achieve the same ends? (That is, successfully detecting that the .vimrc file is being used on Mac OS X.)
推荐答案
我更新的 .vimrc
现在使用以下内容:
My updated .vimrc
now uses the following:
if has("gui_running")
" Gvim
if has("gui_gtk2") || has("gui_gtk3")
" Linux GUI
elseif has("gui_win32")
" Win32/64 GVim
elseif has("gui_macvim")
" MacVim
else
echo "Unknown GUI system!!!!"
endif
else
" Terminal vim
endif
我的原答案如下
你可以试试我在我的 .vimrc 中所做的:
You could try what I do in my .vimrc:
if has("unix")
let s:uname = system("uname -s")
if s:uname == "Darwin"
" Do Mac stuff here
endif
endif
虽然,为了完全透明,我的实际 .vimrc 内容如下:
Although, to be completely transparent, my actual .vimrc reads:
let s:uname = system("echo -n "$(uname)"")
if !v:shell_error && s:uname == "Linux"
主要用于检测 Linux(相对于 OSX)
Mainly for detecting Linux (as opposed to OSX)
我不确定你是否绝对必须这样做 echo -n "$(uname)"
东西,但它与 uname
调用.您的里程可能会有所不同.
I'm not sure if you absolutely have to do that echo -n "$(uname)"
stuff, but it had to do with the newline at the end of the uname
call. Your mileage may vary.
这篇关于如何在我的 .vimrc 文件中检测 OS X,因此某些配置仅适用于 OS X?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!