如何在.vimrc文件中检测OS X,因此某些配置仅适用于OS X? [英] how do I detect OS X in my .vimrc file, so certain configurations will only apply to OS X?

查看:90
本文介绍了如何在.vimrc文件中检测OS X,因此某些配置仅适用于OS X?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在笔记本电脑(OS X)和多台服务器(Solaris& Linux)上使用.vimrc文件,并且假设有一天可以在Windows机器上使用它.我知道一般如何检测UNIX和Windows,但是如何检测OS X? (就此而言,有没有办法区分Linux和Solaris等.并且是否有所有"可以使用的所有字符串的清单?我的

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")似乎很显然像它应该工作,但由于某种原因却没有. (也许苹果公司没有正确地编译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.)

无论如何,我想我需要转移问题的重点:有人能解决这个问题吗? (也就是说,成功检测到Mac OS X上正在使用.vimrc文件.)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆