简体   繁体   English

如何在我的.vimrc文件中检测OS X,因此某些配置仅适用于OS X?

[英]how do I detect OS X in my .vimrc file, so certain configurations will only apply to OS X?

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. 我在我的笔记本电脑(OS X)和几个服务器(Solaris和Linux)上使用我的.vimrc文件,并且假设有一天可以在Windows机器上使用它。 I know how to detect unix generally, and windows, but how do I detect OS X? 我知道如何检测unix和windows,但我如何检测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.) (就此而言,有没有办法区分Linux和Solaris等等。是否有一个列表可以采用所有字符串?我的Google-fu什么都没发现。)

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

But clearly "mac" is not the right string, nor are any of the others I tried. 但显然“mac”不是正确的字符串,也不是我试过的其他字符串。


UPDATE: The answer below ("macunix") seems fairly clearly like it should work, but for some reason it doesn't. 更新:下面的答案(“macunix”)似乎相当清楚它应该工作,但由于某种原因它不会。 (Perhaps Apple didn't compile vim properly to respond to this? Seems unlikely.) (也许Apple没有正确编译vim来回应这个?似乎不太可能。)

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文件正在Mac OS X上使用。)

My updated .vimrc now uses the following: 我更新的.vimrc现在使用以下内容:

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

My original answer is below 我原来的答案如下


You could try what I do in my .vimrc: 你可以尝试我在.vimrc中做的事情:

if has("unix")
  let s:uname = system("uname -s")
  if s:uname == "Darwin"
    " Do Mac stuff here
  endif
endif

Although, to be completely transparent, my actual .vimrc reads: 虽然,为了完全透明,我的实际.vimrc读取:

let s:uname = system("echo -n \"$(uname)\"")
if !v:shell_error && s:uname == "Linux"

Mainly for detecting Linux (as opposed to OSX) 主要用于检测Linux(而不是OSX)

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. 我不确定你是否绝对必须做那个echo -n \\"$(uname)\\"东西,但它与uname调用结束时的换行符有关。 Your mileage may vary. 你的旅费可能会改变。

I could not edit previous answer by adding two character only: 我只能通过添加两个字符来编辑以前的答案:

Here is correct one(passed on my macos 10.6 and default vim console version) 这是正确的(传递我的macos 10.6和默认的vim控制台版本)

if has("unix")
  let s:uname = system("uname")
  if s:uname == "Darwin\n"
    " Do Mac stuff here
  endif
endif

system("uname") will come up with a return character, which makes second if condition failed. system(“uname”)将出现一个返回字符,如果条件失败则返回第二个字符。 Just a small fix to add "\\n". 只是一个小修补程序添加“\\ n”。

I'm doing the same thing you are. 我做的和你一样。 Don't try to detect the OS. 不要试图检测操作系统。 Instead, try to detect the type of vi/vim. 相反,尝试检测vi / vim的类型。

Check :h feature-list for a full list of the conditionals you can use. 检查:h feature-list ,查看可以使用的条件的完整列表。

Here's what I use to detect MacVim in my vimrc: 这是我用来检测我的vimrc中的MacVim的内容:

if has("gui_macvim")
  set guifont=Monaco:h13
endif

With this, you can detect for gvim, vi, vim, and whatever other flavors you might use. 有了这个,您可以检测gvim,vi,vim以及您可能使用的其他任何风格。 The nice thing is that you could have vim-compatible settings on OS X. 好的是你可以在OS X上拥有vim兼容的设置。

Reference from Vim Mailing list 从Vim邮件列表中引用

EDIT: This approach and its variants ( has('mac') , has('macunix') , has('gui_mac') )do not work for vim in OS X. If you use only use MacVim, you're safe. 编辑:这种方法及其变体( has('mac')has('macunix')has('gui_mac') )不适用于OS X中的vim。如果你只使用MacVim,那么你就是安全的。 If you're weird like me and like to occasionally jump into vim, one of the other solutions may be more suitable. 如果你像我一样奇怪并喜欢偶尔跳进vim,其他一个解决方案可能更合适。

homebrew vim and MacVim returns true for has('mac') , however so does has('unix') . homebrew vimMacVim对于has('mac')返回true,但是has('unix') so to have it work across all unix platforms, a possible solution is: 为了让它在所有unix平台上运行,可能的解决方案是:

if has('unix')
  if has('mac')       " osx
    set guifont=...
  else                " linux, bsd, etc
    set guifont=...
  endif
elseif has('win32') || has('win64')
  set guifont=...
endif

on the other hand, as of el capitan, the system vim returns false for has('mac') , and uname snooping is probably the way to go. 另一方面,就像el capitan一样,系统vimhas('mac')返回false,而uname snooping可能是要走的路。 it's an ancient version, never used it. 这是一个古老的版本,从未使用它。

You want macunix . 你想要macunix To quote :h feature-list : 引用:h feature-list

mac     Macintosh version of Vim.
macunix Macintosh version of Vim, using Unix files (OS-X).

mac, AFAIK, only applies to old-school Macs, where \\r is the line separator. mac,AFAIK,仅适用于老式Mac,其中\\ r是行分隔符。

This is the easiest way I have found. 这是我找到的最简单的方法。

if system('uname -s') == "Darwin\n"
  "OSX
  set clipboard=unnamed 
else
  "Linux
  set clipboard=unnamedplus
endif

gui_macvim gui_gtk2 gui_gtk gui_win32 gui_macvim gui_gtk2 gui_gtk gui_win32

There is a OS detection script somewhere on stackoverflow - more keywords to find it: win64 win95 macunix... stackoverflow上有一个操作系统检测脚本 - 找到它的更多关键字:win64 win95 macunix ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM