Skip to content

Nested Configurations

If you are using the lua/ folder to organize your Neovim configuration, your current setup might look something like this:

  • init.lua
  • lazy-lock.json
  • Directorylua
    • keymaps.lua
    • settings.lua
    • mason-setup.lua
    • treesitter-setup.lua
    • lualine-setup.lua
    • plugins-setup.lua

And your init.lua file might have a huge amount of imports, for example:

require("keymaps")
require("settings")
require("treesitter-setup.lua")
require("mason-setup.lua")
require("plugins-setup.lua")
require("lualine-setup.lua")

This may still feel a little unorganized. There are a couple methods to clean up your Lua configuration, as follows.

Using directories and dot notation

With Lua modules, you can create subdirectories within your lua/ directory. For example:

  • init.lua
  • lazy-lock.json
  • Directorylua
    • Directorycore
      • keymaps.lua
      • settings.lua
    • Directorylsp
      • mason-setup.lua
      • treesitter-setup.lua
    • Directoryplugins
      • lualine-setup.lua
      • plugins-setup.lua

Which cleans up the lua/ directory nicely.

To relfect these changes, in your init.lua you will need to update the require statements to include the subdirectories.

require("core.keymaps")
require("core.settings")
require("lsp.treesitter-setup.lua")
require("lsp.mason-setup.lua")
require("plugins.plugins-setup.lua")
require("plugins.lualine-setup.lua")

However, our init.lua file is still the same. If this works for you - great! If you want to clean up your init.lua file as well, read the next method.

Using directories and init.lua files

This takes a similar approach to the previous method, but in each lua/ subdirectory, you also create an init.lua file.

  • init.lua
  • lazy-lock.json
  • Directorylua
    • Directorycore
      • init.lua
      • keymaps.lua
      • settings.lua
    • Directorylsp
      • init.lua
      • mason-setup.lua
      • treesitter-setup.lua
    • Directoryplugins
      • init.lua
      • lualine-setup.lua
      • plugins-setup.lua

In each respective nested init.lua, you can require that directory’s files.

For example, in plugins/init.lua your code would be:

require("lualine-setup")
require("plugins-setup")

And the same pattern follows for each lua/ subdirectory.

In your top-level init.lua file, you can now use the following require statements:

require("core")
require("lsp")
require("plugins")

Which makes the configuration a lot cleaner.