OS detection in Vim

As somebody that jumps between multiple operating systems, it’s become necessary to have OS detection in my VIm configuration. This problem isn’t new to me, as I’ve already taken strides in my dotfiles and have previously talked about OS detection in shell scripts.

OS detection in Vim

Vim has a function that allow you to check if the currently running Vim has certain features. This function can also be used to detect with OS you’re running on. Here’s how to conditionally do stuff based on which operating system you’re on:

if has('unix')
  " Linux stuff
elseif has('mac')
  " macOS stuff
elseif has('win32')
  " Gross stuff    
endif
VimL

Linux distro detection in Vim

While OS detection is great, sometimes you need to go a bit deeper. Different Linux distributions have their own unique quirks, so being able to detect which one you’re on can be necessary. Similar to the function we used to check on features, we can use a different function to detect if certain executables are present on the system:

if executable('apt')
  " Debian stuff
elseif executable('pacman')
  " Arch stuff
elseif executable('yum')
  " Gross stuff
endif
VimL
Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.