How do I write vimrc configuration that works in both Vim and Neovim by detecting which one is running?
Answer
has('nvim')
Explanation
The has() function tests whether a feature or capability is available, returning 1 (true) or 0 (false). It is the standard tool for writing portable .vimrc / init.vim configuration that adapts to different Vim versions, platforms, and compiled features — including detecting Neovim.
How it works
has({feature}) accepts a feature name and returns 1 if available. Common guards:
has('nvim')— running in Neovimhas('python3')— Python 3 support compiled inhas('job')— async job control (Vim 8+)has('timers')— timer support (Vim 8+)has('patch-8.2.4919')— specific Vim patch levelhas('unix')/has('win32')— OS platformhas('gui_running')— inside a GUI (gVim, MacVim)
Example
Apply settings specific to each editor:
if has('nvim')
" Neovim-only: live preview of substitutions
set inccommand=split
tnoremap <Esc> <C-\><C-n>
else
" Vim 8-only: built-in terminal size
set termwinsize=12x0
endif
Guard against missing features before using them:
if has('timers')
call timer_start(500, {-> execute('checktime')})
endif
if has('patch-9.0.0185')
set smoothscroll
endif
Tips
- Test any feature string interactively:
:echo has('nvim') - Full list of valid feature names:
:help feature-list - For precise version checks,
has('patch-X.Y.Z')is more reliable than comparingv:versiondirectly