How can I distinguish current operating system in my .vimrc?

20

7

I use Vim on both OS X and Windows, with almost identical configuration.

I would like to be able to use the same .vimrc on the two operating systems, but there are a couple of things I need to configure differently.

What I'd like to add to my .vimrc file is:

:if <windows>
  "some windows-specific settings here
:elseif <os x>
  "some os x-specific settings here
:endif

But I don't know what to use for <windows> and <os x>.

Is this possible?

Rich

Posted 2010-09-27T11:58:28.503

Reputation: 2 000

Answers

30

N.B. Although both previous answers gave me enough information to figure out how to solve the problem (and received upvotes from me), neither actually gave the full answer. So that others with the same question don't have to do the research, I'm adding my own answer. However, if @googletorp or @Azz edit their answer to include this info, I'll remove my answer and accept theirs.

The output to :h feature-list suggests that you should be able to use has("win32") and has("macunix"), but the latter doesn't work in the version of Vim included in OS X. (It does, however, work in MacVim.)

Here is what I ended up using:

if has("win32")
  "Windows options here
else
  if has("unix")
    let s:uname = system("uname")
    if s:uname == "Darwin\n"
      "Mac options here
    endif
  endif
endif

Note that has("win32") worked for me, even in 64 bit Vim on 64 bit Windows.

You could also use similar tests of uname within the if has("unix") block to distinguish other flavours of Unix. Just run uname or uname -a from the command-line to see what you need to compare s:uname with. See also :h matchstr() if you need to compare just a part of uname's output.

Rich

Posted 2010-09-27T11:58:28.503

Reputation: 2 000

1For record: in vim from MSYS2 has("unix") is 1, has("win32") is 0 and has("win32unix") is 1. So use has("win32unix") to distinguish it. – user31389 – 2018-11-06T13:10:20.650

4

You can take a look here

Basically, you can use either has(), system():

let os = substitute(system('uname'), "\n", "", "")
if os == "SunOS"
  ..
endif  

googletorp

Posted 2010-09-27T11:58:28.503

Reputation: 528

2

This seems to be what you're after, I don't quite understand it so I'll just link you.

https://stackoverflow.com/questions/2842078/how-do-i-detect-os-x-in-my-vimrc-file-so-certain-configurations-will-only-apply

Azz

Posted 2010-09-27T11:58:28.503

Reputation: 3 777

Strange. That seems to be scraped from http://stackoverflow.com/questions/2842078

– Rich – 2010-09-27T15:13:01.080

1@Rich: All SU data is licensed under Creative Commons Attribution ShareAlike, which they seem to adhere to. – Daniel Beck – 2010-09-28T15:17:35.070

@Daniel: Interesting! Thanks for letting me know. – Rich – 2010-09-30T10:44:27.963