Skip to content

Extending The Configuration

So we have an init.lua file with some initial settings, but we probably don’t want to store our entire configuration in here.

Why?

If you want to add plugins, custom keybinds, and other features, keeping it in a single file can get a bit messy. In this instance, you may want to swap to a different approach to keep your configuration more organized.

The Lua directory

Some of the previous directories we saw — for your keymaps, color schemes, and plugins — might seem appealing to you for organization.

However, we recommend setting up your configuration in the lua/ directory.

Why The Lua Directory?

If you’ve ever looked at other Neovim configurations or tutorials, this is the directory that you’ve probably seen the most.

Here are the two main reasons to opt for the lua/ directory over the other individual runtime directories:

  • Better organization and modularity so everything is in one place.
  • lua/ can run any valid Lua code you want → much more flexible.

Moving The Initial Configuration Code

Let’s take the following code from our Writing an Initial Configuration section.

local opt = vim.opt
opt.cursorline = true
opt.number = true
opt.relativenumber = true
opt.termguicolors = true

Remove this code from your init.lua file in preparation to move it into your new lua/ configuration.

Then perform the following steps from within your ~/.config/nvim directory.

Terminal window
mkdir lua
cd lua
touch settings.lua

This code:

  1. Creates the lua/ directory
  2. Changes into the directory
  3. Creates a new file called settings.lua

Your .config/nvim folder should now look something like this:

  • Directorynvim
    • init.lua
    • Directorylua/
      • settings.lua

Using Your New Configuration and Lua Modules

When you create configuration files in the lua/ directory, Neovim doesn’t automatically run them.

Files in this directory are treated as Lua modules, so they must be imported into our original init.lua file to be used.

Importing the Configuration File

To do this, simply add the following line to your init.lua file:

require("settings")

Notice that you do not need to pass in the entire file path, or the .lua extension.

The Basis of Neovim Configuration

The steps you have just taken from the basis of any large Neovim configuration. Great job!

It’s up to you to decide which files you want to create in your lua/ directory and what to store where - and don’t forget to require them in your init.lua file.

Here’s an example of what a more complex setup may look like:

  • Directorycore
    • keymaps.lua
    • settings.lua
  • Directorylsp
    • mason-setup.lua
    • treesitter-setup.lua
  • Directoryplugins
    • autopairs-setup.lua
    • gitsigns-setup.lua
    • lualine-setup.lua
    • nvim-tree.lua
    • plugins-setup.lua
    • tsautotag-setup.lua

Which we’ll get into configuring!