Course Lessons
Breaking up is easy to do
We've done so much, but looking at our init.lua, it's hard to imagine this not getting confusing really quickly. Single responsibility — this isn't that.
So let's break up. Let me show you how — it's easy to do.
First, let's remember how we currently setup plugins.
local opts = {}
local plugins = {
{ "catppuccin/nvim", name = "catppuccin", priority = 1000 },
{
'nvim-telescope/telescope.nvim', tag = '0.1.6',
dependencies = { 'nvim-lua/plenary.nvim' }
},
{"nvim-treesitter/nvim-treesitter", build = ":TSUpdate"},
{
"nvim-neo-tree/neo-tree.nvim",
branch = "v3.x",
dependencies = {
"nvim-lua/plenary.nvim",
"nvim-tree/nvim-web-devicons",
"MunifTanjim/nui.nvim",
}
}
}
require("lazy").setup(plugins, opts)- We define
optsas an empty lua table. - We define
pluginsas a nested lua table. For each plugin, we add another table.
By convention, we can create a file called plugins.lua in a lua directory. If this exists and returns a lua table like we have in our init.lua, we'd be in business.
To utilize this, we need to do a few things.
- Update the
require("lazy")line from thisrequire("lazy").setup(plugins, opts)torequire("lazy").setup("plugins") - Create the
lua/plugins.luafile. - Move our plugins lua table to the
plugins.luafile.
After you've updated the require("lazy") line in init.lua, let's move on to creating the plugins.lua file. You can do this via Neotree!
control+n- open Neotreea- add a new file/directory- type
lua/plugins.luato create the file
Once created, we should select and delete the entire block defining the plugins lua table. Open the new file via Neotree (control+n) and paste the lua table.
We won't need to define a local variable plugins here. We can just return it instead. It should now look like this.
return {
{ "catppuccin/nvim", name = "catppuccin", priority = 1000 },
{
'nvim-telescope/telescope.nvim', tag = '0.1.6',
dependencies = { 'nvim-lua/plenary.nvim' }
},
{"nvim-treesitter/nvim-treesitter", build = ":TSUpdate"},
{
"nvim-neo-tree/neo-tree.nvim",
branch = "v3.x",
dependencies = {
"nvim-lua/plenary.nvim",
"nvim-tree/nvim-web-devicons",
"MunifTanjim/nui.nvim",
}
}
}Once this is done, let's close Neovim and reopen it. Everything should still work as before. Let's try opening Neotree with <C-n>. Good? Great!
But what does this really get us? We still have one big plugins file.
Great Question!
By having Lazy use require("lazy").setup("plugins"), it will look for any lua files within the lua directory. As long as each file returns a Lua table, it will load and merge these together. Even better, it recognizes when a file within this directory is updated and will reflect those changes on save!
So what's next? It's time to actually break up our plugins!
You've completed this lesson!
You completed this lesson less than a minute ago.