How can I detect OS X in my .vimrc file, so some configurations only apply to OS X?

I use my .vimrc file on my laptop (OS X) and several servers (Solaris and Linux), and could hypothetically someday use it in a Windows window. I know how to detect unix in general and windows, but how to detect OS X? (And in this regard, is there a way to distinguish between Linux and Solaris, etc. And is there a list of all the lines that may matter? My Google-fu didnโ€™t display anything.)

For example, I would 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 it is clear that "mac" is not the correct line, and none of the others that I have tried.




UPDATE: The answer below ("macunix") looks pretty clear, as if it should work, but for some reason it is not. (Perhaps Apple did not compile vim correctly to answer this? It seems unlikely.)

In any case, I think I need to focus on the question: does anyone have a solution that will achieve the same goals? (That is, it successfully detects that the .vimrc file is used in Mac OS X.)

+46
vim macos
May 15 '10 at 11:24 a.m.
source share
7 answers

You can 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" 

Mostly for Linux detection (unlike OSX)

I'm not sure you absolutely need to do this stuff echo -n \"$(uname)\" , but this is connected to a new line at the end of the uname call. Your mileage may vary.

+46
May 16 '10 at 6:51
source share

I could not edit the previous answer by adding only two characters:

Here is the correct one (passed my macos 10.6 and the vim version for consoles by default)

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

("uname") will have a return character, which does the second if the condition is not met. Just a small fix to add "\ n".

+21
Feb 23 '11 at 15:44
source share

I do the same as you. Do not try to detect the OS. Instead, try to determine the type of vi / vim.

Note :h feature-list complete list of conventions that you can use.

Here is what I use to detect MacVim in my vimrc:

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

With this, you can discover gvim, vi, vim and any other options that you can use. It's nice that you could have vim-compatible settings in OS X.

Vim mailing list link

EDIT: This approach and its variants ( has('mac') , has('macunix') , has('gui_mac') ) do not work for vim on OS X. If you use only MacVim, you are safe if you are strange like me, and sometimes I want to jump into vim, then one of the other solutions may be more suitable.

+13
Feb 08 2018-12-12T00:
source share

Do 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, applies only to old-school computers, where \ r is the line separator.

+9
May 15, '10 at 23:28
source share

homebrew vim and MacVim returns true for has('mac') , but also has('unix') . therefore, for it to work on all unix platforms, a solution is possible:

 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, like el capitan, the vim system returns false for has('mac') , and uname tracking is probably the way to go. this is an ancient version that never used it.

+8
Jan 25 '16 at 10:26
source share

This is the easiest way I've found.

 if system('uname -s') == "Darwin\n" "OSX set clipboard=unnamed else "Linux set clipboard=unnamedplus endif 
+2
Sep 04 '16 at 3:25
source share

gui_macvim gui_gtk2 gui_gtk gui_win32

There is an OS detection script somewhere on stackoverflow - more keywords to find it: win64 win95 macunix ...

-2
May 08 '12 at 10:01
source share



All Articles