How to make VIM settings computer-dependent in .vimrc?

I share my VIM configuration file between several computers. However, I want some settings to be specific for certain computers.

For example, font sizes on the high-res laptop should be different to the low-res desktop. And more importantly, I want gVIM on Windows to behave more windowsy and MacVim on OSX to behave more maccy and gVIM on Linux to just behave like it always does. (That might be a strange sentiment, but I am very used to switch mental modes when switching OSes)

Is there a way to make a few settings in the .vimrc machine- or OS-dependent?


OS detection in .vimrc:

if has('win32')
    ......
elseif has('mac')
    ......
elseif has('unix')
    ......
endif

To test for a particular machine, you can test the output of the hostname command. For example,

let hostname = substitute(system('hostname'), '\n', '', '')
if hostname == "tigger"
   ...
elseif hostname == "pooh"
   ...
endif

You could also test the value of available environment variables:

if $HOSTNAME == "tigger"
   ...
elseif $HOSTNAME == "pooh"
   ...
endif

The $DISPLAY variable can also be useful.


I have this snippet in my vimrc:

let s:host_vimrc = $HOME . '/.' . hostname() . '.vimrc'                                                                                                                               
if filereadable(s:host_vimrc)                                                                                                                                                        
  execute 'source ' . s:host_vimrc                                                                                                                                                   
endif

This simply executes source $HOME/.$HOSTNAME.vimrc if it exists. I've used hostname() and concatenation, you could also use the more succinct expand('$HOME/.$HOSTNAME.vimrc') if you know that $HOSTNAME is always set.


The previous answer about OS detection does not detect OS X in MacVim for me (and neither does using has("macunix") as the documentation suggests it should).

Here's what I use to distiguish between Windows and OS X:

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.


With a lot of machines, listing all hostnames in .vimrc can become tedious, it might be easier to distinguish between different unix flavors:

" set font when running on Solaris
if (match(system('uname -s'), 'SunOS') >= 0)  
   set guifont=*   " fixes "E665: Cannot start GUI, no valid font found"
endif