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

44

17

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?

bastibe

Posted 2010-10-01T09:46:37.927

Reputation: 3 544

Answers

44

OS detection in .vimrc:

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

Casual Coder

Posted 2010-10-01T09:46:37.927

Reputation: 3 614

A very late answer, but yes, has('mac') triggers in OSX for me. – Diablo-D3 – 2015-03-28T09:13:00.250

1I've tested vim 1) that comes with OS X, and 2) that I installed via Gentoo Prefix (something similar to Homebrew), and 3) that comes from MacVim. Only the one comes from MacVim will correctly return true for has("mac"). My guess is that they used some patch that actually make the feature test work. If you do rely on has("mac") test, do some test to make sure it's supported on your vim. – yegle – 2015-10-10T04:26:28.830

Note that you have to test has('mac') and has('unix') in the order above, because a Mac also has unix! – Giles Gardam – 2017-05-27T13:40:05.443

4Also useful: has('gui_running') if you need to differentiate between tty mode and GUI mode. – Chris Johnsen – 2010-10-01T13:20:08.350

10The has() function tests the presence of Vim features. There is no 'linux' feature. The proper argument is 'unix'. Also, the proper argument for OS-X is 'macunix'. There is also a 'mac' feature, but I don't know whether has('mac') is true for all Macs or just pre-OS-X Macs. See :help feature-list for the full list. – garyjohn – 2010-10-01T15:14:44.140

1Yes, has('unix'). My mistake. – Casual Coder – 2010-10-01T17:14:16.277

26

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.

garyjohn

Posted 2010-10-01T09:46:37.927

Reputation: 29 085

The result for system('hostname') was machine.mycompany.com\n, so I had to change the substitute command to substitute(system('hostname'), '\.\_.*$', '', ''). – JPaget – 2015-10-20T18:35:57.533

13hostname() will do this, eg: if hostname() == 'tigger'..., without having to make the system call. – tvon – 2011-04-15T16:03:37.653

9

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.

unthought

Posted 2010-10-01T09:46:37.927

Reputation: 191

7

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.

Rich

Posted 2010-10-01T09:46:37.927

Reputation: 2 000

2

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

philant

Posted 2010-10-01T09:46:37.927

Reputation: 135

Unfortunately, this breaks on windows machines. – oligofren – 2015-08-23T19:26:24.520

1

You could just put the OS-specific stuff in a custom .gvimrc for each machine, and use a common .vimrc on all of them. Both files are read when GVim starts, only .vimrc is read when the non-gui Vim starts.

Neil

Posted 2010-10-01T09:46:37.927

Reputation: 412

1

Regarding separation between Linux and Windows - you can simply put different settings in .vimrc and _vimrc, accordingly.

bjauy

Posted 2010-10-01T09:46:37.927

Reputation: 276