< Back to frontpage

vim functions to detect OS and platform


for creating a cross-platform vim configuration

You want to use one vim configuration across multiple machines: Linux, macOS, FreeBSD, even windows.

There are a few ways you can set up a vim configuration that works across operating systems. One option is spf13/spf13-vim.

I prefer a middle ground. I like some elements of spf13's configuration, but prefer to be methodical on what I add to keep things lean as possible. I maintain my own vim configuration at tony/vim-config-framework.

This snippet is derived from spf13-vim (see the original blog post). License Apache 2.

OS / Platform detection

This is from spf13-vim's .vimrc.

" Environment {
    " Identify platform {
        silent function! OSX()
            return has('macunix')
        endfunction
        silent function! LINUX()
            return has('unix') && !has('macunix') && !has('win32unix')
        endfunction
        silent function! WINDOWS()
            return  (has('win16') || has('win32') || has('win64'))
        endfunction
    " }
" }

This allows your vim configuration to use the functions OSX(), LINUX(), WINDOWS(), and FREEBSD() to configure based on the environment vim is running.

We can make it a bit more cross platform. Let's add support for FreeBSD and clean up the indentation / comments a bit:

" Platform identification {
    silent function! OSX()
        return has('macunix')
    endfunction
    silent function! LINUX()
        return has('unix') && !has('macunix') && !has('win32unix')
    endfunction
    silent function! WINDOWS()
        return  (has('win16') || has('win32') || has('win64'))
    endfunction
    silent function! FREEBSD()
      let s:uname = system("uname -s")
      return (match(s:uname, 'FreeBSD') >= 0)
    endfunction
" }

Let's see some examples of how functions like this can be implemented.

Usage

This requires the snippet above since it will use the platform-detection functions. Here is how spf13-vim uses it the first time:

" Environment {
    " Basics {
        set nocompatible        " Must be first line
        if !WINDOWS()
            set shell=/bin/sh
        endif
    " }
" }

It's using !WINDOWS() to infer that the platform is Unix-like. If only we had a way to make it more concise to the reader:

" Platform identification {
    silent function! OSX()
        return has('macunix')
    endfunction
    silent function! LINUX()
        return has('unix') && !has('macunix') && !has('win32unix')
    endfunction
    silent function! WINDOWS()
        return  (has('win16') || has('win32') || has('win64'))
    endfunction
    silent function! UNIXLIKE()
        return !WINDOWS()
    endfunction
    silent function! FREEBSD()
      let s:uname = system("uname -s")
      return (match(s:uname, 'FreeBSD') >= 0)
    endfunction
" }

With UNIXLIKE(), we can do:

" Basics {
    set nocompatible        " Must be first line
    if UNIXLIKE()
        set shell=/bin/sh
    endif
" }

Here's how I use it to load freebsd's vim configuration settings:

if FREEBSD()
  call SourceIfExists("~/.vim/compat/freebsd.vim")
  call SourceIfExists("/usr/src/tools/tools/editing/freebsd.vim")
end

If you want to learn more about SourceIfExists check out :postvIIMz8vZ.