vimtricks.wiki Concise Vim tricks, one at a time.

How do I check if a specific Vim feature or capability is available before using it in my vimrc?

Answer

has('feature')

Explanation

The has('feature') function returns 1 if the specified feature is available in the current Vim/Neovim instance, 0 otherwise. It enables writing portable vimrc files that enable functionality only when the underlying build supports it — preventing errors on minimal or cross-platform installations.

How it works

Common feature strings:

  • has('nvim') — running in Neovim (not standard Vim)
  • has('python3') — Python 3 scripting support is compiled in
  • has('clipboard') — system clipboard integration is available
  • has('timers')timer_start() async timers are supported
  • has('terminal'):terminal built-in terminal is available
  • has('patch-8.2.0') — Vim is at least version 8.2 patch 0

Use it in if guards in your vimrc:

if has('clipboard')
  set clipboard=unnamedplus
endif

Example

A vimrc snippet that sets Neovim-specific options without breaking standard Vim:

if has('nvim')
  set inccommand=split    " live substitution preview
endif

if has('python3')
  let g:python3_host_prog = '/usr/bin/python3'
endif

Tips

  • :echo has('nvim') — run interactively to test any feature name
  • has('gui_running') — detect GVim / MacVim for GUI-specific mappings
  • Use exists('*funcname') to check if a specific function is defined: exists('*complete_info')
  • Use exists(':command') to check if a command is available: exists(':NvimTreeToggle')
  • :help feature-list shows the full list of valid feature names for has()

Next

How do I jump to a tag in Vim and automatically choose if there is only one match?