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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2842078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:57:02  来源:igfitidea点击:

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

macosvim

提问by iconoclast

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-futurned up nothing.)

我在我的笔记本电脑 (OS X) 和几台服务器 (Solaris & Linux) 上使用我的 .vimrc 文件,并且假设有一天可以在 Windows 机器上使用它。我知道如何检测 unix 和 windows,但如何检测 OS X?(就此而言,有没有办法区分 Linux 和 Solaris 等。是否有一个列表列出了“has”可以接受的所有字符串?我的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 shouldwork, but for some reason it doesn't. (Perhaps Apple didn't compile vim properly to respond to this? Seems unlikely.)

更新:下面的答案(“macunix”)似乎很明显应该可以工作,但由于某种原因它没有。(也许 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 上使用。)

回答by rossipedia

My updated .vimrcnow 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 unamecall. Your mileage may vary.

我不确定您是否绝对必须这样做echo -n \"$(uname)\",但这与uname通话结束时的换行符有关。你的旅费可能会改变。

回答by Gene Wu

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. Just a small fix to add "\n".

system("uname") 将提供一个返回字符,如果条件失败,则返回第二个字符。只是添加“\n”的一个小修复。

回答by Eric Hu

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-listfor 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. The nice thing is that you could have vim-compatible settings on OS X.

有了这个,您可以检测 gvim、vi、vim 以及您可能使用的任何其他风格。好消息是您可以在 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. If you're weird like me and like to occasionally jump into vim, one of the other solutions may be more suitable.

编辑:此方法及其变体 ( has('mac'), has('macunix'), has('gui_mac')) 不适用于 OS X 中的 vim。如果您只使用 MacVim,则很安全。如果你像我一样古怪,喜欢偶尔跳入 vim,其他解决方案之一可能更合适。

回答by minusf

homebrew vimand MacVimreturns true for has('mac'), however so does has('unix'). so to have it work across all unix platforms, a possible solution is:

homebrewvim并为MacVim返回 true has('mac'),但也是如此has('unix')。为了让它在所有 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 vimreturns false for has('mac'), and unamesnooping is probably the way to go. it's an ancient version, never used it.

另一方面,从 el capitalan 开始,系统为vim返回 false has('mac'),而uname窥探可能是要走的路。这是一个古老的版本,从未使用过。

回答by Michael Madsen

You want macunix. To quote :h feature-list:

你要macunix。引用: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 是行分隔符。

回答by Weston Ganger

This is the easiest way I have found.

这是我找到的最简单的方法。

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

回答by skurczybyk

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 ...