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 inhas('clipboard')— system clipboard integration is availablehas('timers')—timer_start()async timers are supportedhas('terminal')—:terminalbuilt-in terminal is availablehas('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 namehas('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-listshows the full list of valid feature names forhas()