4

Say I do not quite like the shipped Lua indentexpr (GetLuaIndent(), defined in $VIMRUNTIME/indent/lua.vim). With vim, I can disable or change it using this minimal configuration:

~ % cat .vimrc
filetype indent on
filetype plugin on
~ % tree .vim/
.vim/
└── after
    └── ftplugin
        └── lua.vim

3 directories, 1 file
~ % cat .vim/after/ftplugin/lua.vim
setlocal indentexpr=

So next time I open up a Lua file, executing :set indentexpr? returns indentexpr=, and :scriptnames gives something like this:

:scriptnames
  1: /Applications/MacVim.app/Contents/Resources/vim/vimrc
  2: ~/.vimrc
  3: /Applications/MacVim.app/Contents/Resources/vim/runtime/filetype.vim
  4: /Applications/MacVim.app/Contents/Resources/vim/runtime/indent.vim
  5: /Applications/MacVim.app/Contents/Resources/vim/runtime/ftplugin.vim
  6: /Applications/MacVim.app/Contents/Resources/vim/runtime/plugin/getscriptPlugin.vim
  7: /Applications/MacVim.app/Contents/Resources/vim/runtime/plugin/gzip.vim
  8: /Applications/MacVim.app/Contents/Resources/vim/runtime/plugin/logiPat.vim
  9: /Applications/MacVim.app/Contents/Resources/vim/runtime/plugin/manpager.vim
 10: /Applications/MacVim.app/Contents/Resources/vim/runtime/plugin/matchparen.vim
 11: /Applications/MacVim.app/Contents/Resources/vim/runtime/plugin/netrwPlugin.vim
 12: /Applications/MacVim.app/Contents/Resources/vim/runtime/plugin/rrhelper.vim
 13: /Applications/MacVim.app/Contents/Resources/vim/runtime/plugin/spellfile.vim
 14: /Applications/MacVim.app/Contents/Resources/vim/runtime/plugin/tarPlugin.vim
 15: /Applications/MacVim.app/Contents/Resources/vim/runtime/plugin/tohtml.vim
 16: /Applications/MacVim.app/Contents/Resources/vim/runtime/plugin/vimballPlugin.vim
 17: /Applications/MacVim.app/Contents/Resources/vim/runtime/plugin/zipPlugin.vim
 18: /Applications/MacVim.app/Contents/Resources/vim/runtime/indent/lua.vim
 19: /Applications/MacVim.app/Contents/Resources/vim/runtime/ftplugin/lua.vim
 20: ~/.vim/after/ftplugin/lua.vim

(no idea why $VIMRUNTIME uses MacVim's, but at least $VIMRUNTIME/indent/lua.vim was sourced before ~/.vim/after/ftplugin/lua.vim)


I then tried to replicate this behaviour in neovim, having:

.config % tree nvim/
nvim
└── after
    └── ftplugin
        └── lua.lua

3 directories, 1 file
.config % cat nvim/after/ftplugin/lua.lua
vim.bo.indentexpr = ''

Not effective as :set indentexpr? still gives indentexpr=GetLuaIndent() as before. Surprisingly and unsurprisingly, it turns out that $VIMRUNTIME/indent/lua.vim, this time, was sourced AFTER my custom ftplugin:

:scriptnames
  1: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/ftplugin.vim
  2: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/indent.vim
  3: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/filetype.lua
  4: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/syntax/syntax.vim
  5: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/syntax/synload.vim
  6: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/gzip.vim
  7: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/health.vim
  8: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/matchit.vim
  9: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/pack/dist/opt/matchit/plugin/matchit.vim
 10: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/matchparen.vim
 11: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/netrwPlugin.vim
 12: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/rplugin.vim
 13: ~/.local/share/nvim/rplugin.vim
 14: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/shada.vim
 15: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/spellfile.vim
 16: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/tarPlugin.vim
 17: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/tohtml.vim
 18: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/tutor.vim
 19: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/zipPlugin.vim
 20: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/editorconfig.lua
 21: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/man.lua
 22: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/plugin/nvim.lua
 23: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/ftplugin/lua.vim
 24: ~/.config/nvim/after/ftplugin/lua.lua
 25: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/indent/lua.vim
 26: /opt/homebrew/Cellar/neovim/0.9.0/share/nvim/runtime/syntax/lua.vim

Should I expect this? No idea. But since my ftplugin will be sourced earlier, maybe I can skip sourcing $VIMRUNTIME/indent/lua.vim by setting up the b:did_indent variable:

vim.cmd[[
if exists("b:did_indent")
  finish
endif
let b:did_indent = 1
]]
vim.bo.indentexpr = ''
print('set b:did_indent to ' .. vim.b.did_indent)

...since $VIMRUNTIME/indent/lua.vim starts with this:

if exists("b:did_indent")
  finish
endif
let b:did_indent = 1

Then I opened a Lua file and saw set b:did_indent to 1, however :set indentexpr? still gave me indentexpr=GetLuaIndent(). I am so confused. Are these expected behaviours? What can I do to effectively customise the indentexpr?

1
  • Seems like a bug to me, personally, but you’d have to find out how the ordering is defined for Lua files. Double-check runtimepath value, though, since that should be what ultimately controls the order.
    – D. Ben Knoble
    Apr 16, 2023 at 12:41

1 Answer 1

1

My recommendation is to move your code into: nvim/after/indent/lua.vim

setlocal indentexpr=

The indent folders are processed after the ftplugin folder and are indented to be used to set the indentation options (like their name suggest).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.