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

7

52

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

But clearly "mac" is not the right string, nor are any of the others I tried.


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

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

Gluten answered 15/5, 2010 at 23:24 Comment(1)
has("macunix") works fine with the version of VIM installed by Homebrew, currently 7.4.1-936. YMMV.Opsonize
R
52

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

My original answer is below


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

Although, to be completely transparent, my actual .vimrc reads:

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

Mainly for detecting Linux (as opposed to 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. Your mileage may vary.

Relieve answered 16/5, 2010 at 6:51 Comment(6)
Maybe other people aren't running in to this problem but I had to change the above line to if s:uname == "Darwin\n"Algie
@JasonAxelson You can remove the \n using substitution: let s:uname = substitute(system("uname"), '\n', '', '')Garniture
@Relieve As it seems you wanted to be portable by testing has("unix"), you should use uname -s instead of uname.Garniture
@abeaumet correct, that would be the most portable way of doing itRelieve
Could instead do a regexp match with if (system('uname') =~ "darwin"). In vim, see ':h =~' for other options, e.g. specificying case-sensitivitySwinton
No need for additional quotes - let s:uname = system("echo -n $(uname -s)")Endocrinotherapy
I
25

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)

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

Isahella answered 23/2, 2011 at 15:44 Comment(0)
B
15

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

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. it's an ancient version, never used it.

EDIT: recent macos has vim 9.x with has(mac) returning true.

Beadruby answered 25/1, 2016 at 10:26 Comment(2)
I have tested, and this is the best solution. I guess it has so few upvotes because it's years newer, but it's a far simpler and equally effective solution.Petulance
Works on Mac with Vim 8 from Brew.Intertype
S
14

I'm doing the same thing you are. Don't try to detect the OS. Instead, try to detect the type of vi/vim.

Check :h feature-list for a full list of the conditionals you can use.

Here's what I use to detect MacVim in my vimrc:

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.

Reference from Vim Mailing list

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.

Sic answered 8/2, 2012 at 20:27 Comment(4)
okay, this looks very useful, but whether I have TextMate available depends entirely on the OS, and not on whether it is a GUI version or a normal version of vim. So for my purposes I'm not sure this will help me much. I have a command to open a file in TextMate, which only applies if I'm on OS X, and I may add similar commands to open the current file in Sublime Text if I'm on Windows or Linux. And I really almost never use GUI version of vim, just plain vim in the terminal. (I find MacVim disorienting: it doesn't behave like a real Cocoa app, and it isn't a terminal app either.)Gluten
Ah, I wasn't aware that TextMate could use vimrc filesSic
TextMate doesn't use .vimrc files, but I put a command in my .vimrc file to open the current file in TextMate. If I'm on OS X I can just type :mate and get the same file in TextMate, and use some of its very handy features, like bundles, etc.Gluten
@Gluten bundles? Say no more! github.com/msanders/snipmate.vim + github.com/scrooloose/snipmate-snippetsWindward
C
9

You want macunix. To quote :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.

Carey answered 15/5, 2010 at 23:28 Comment(4)
Thanks, that seems like it should work, but for some reason it's not. If I put my 'mate' command outside the if-block, it works. But if I put it after 'if has("macunix")' then it fails. Any ideas?Gluten
My guess is that mac refers to old-school Mac OS (before OS X which is Unix-based).Nimitz
Now vim has the 'osx' featuer. See: github.com/vim/vim/blob/…Stardom
@Charles osx is nice indeed, however nvim doesn't have it at the time of writing.Beadruby
I
6

This is the easiest way I have found.

if system('uname -s') == "Darwin\n"
  "OSX
  set clipboard=unnamed 
else
  "Linux
  set clipboard=unnamedplus
endif
Intrusive answered 4/9, 2016 at 3:25 Comment(0)
P
-2

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

Predecease answered 8/5, 2012 at 22:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.