Skip to main content

How to Override Default Options in Neovim

·191 words·1 min

A very common confusion for new users of Neovim is that their option settings for a particular filetype does not work in init.vim (or init.lua).

For non-indent related option #

For non-indent related option, to make sure you option settings have the precedence over builtin. For filetype foo, you can create {NVIM_CONF_DIR}/after/ftplugin/foo.vim or (foo.lua) and write your setting their. For example,

setlocal expandtab
setlocal tabstop=2
setlocal shiftwidth=2
setlocal softtabstop=2

For indent related options #

Someone asked a question on stack overflow about php. The issue is that setting smartindent and indentexpr in ~/.config/nvim/after/ftplugin/php.lua does not work. After opening a php file, smartindent and indentexpr are still set by the php.vim under nvim runtimepath:

$VIMRUNTIME/runtime/indent/php.vim

This is because script under indent/ and syntax/ directory is sourced after script in ftplugin/. The source order is as follows:

" first wave
ftplugin/foo.vim
$VIMRUNTIME/ftplugin/foo.vim
after/ftplugin/foo.vim

" second wave
indent/foo.vim
$VIMRUNTIME/indent/foo.vim
after/indent/foo.vim

" third wave
syntax/foo.vim
$VIMRUNTIME/syntax/foo.vim
after/syntax/foo.vim

So in order for the user’s indent setting to take precedence, we need to put it under ~/.config/after/indent/.

Here is what works for me.

  • create the file ~/.config/nvim/after/indent/php.lua

  • add the settings there:

    vim.o.smartindent = true
    vim.o.indentexpr = ''