Skip to content

Adding a new plugin with lazy.nvim as plugin manager

Prashant Rawat edited this page Aug 12, 2023 · 1 revision

How to add new plugins to the config

Specs in lazy.nvim

Now we use lazy.nvim as a plugin manager and specifically use its specs as a way to add and manage plugins. The specs location is lua/user so lazy will try to load any files located there, so we return plugins as table in every file that lazy trys to load. For more information, check out the lazy.nvim/structuring your plugin documentation.

How would you want your plugin file to look like

I will explain this using the bufferline.lua so this is how it looks like:

-- I have explained what each line does in the above file.
local M = {
  "akinsho/bufferline.nvim", -- plugin location
  commit = "c7492a76ce8218e3335f027af44930576b561013", -- commit hash
  event = { "BufReadPre", "BufAdd", "BufNew", "BufReadPost" }, --events that trigger this plugin
  dependencies = { -- this is used to contain dependencies for this plugin that will be loaded when this plugin is loaded
    {
      "famiu/bufdelete.nvim",
      commit = "8933abc09df6c381d47dc271b1ee5d266541448e",
    },
  },
}

function M.config() -- this is where we configure the plugin
  require("bufferline").setup { -- you call the setup function here of the plugin
    -- inside here will have all the configs for the plugin just like you had in packer
    }
end

return M

Now in your plugin file it's not necessary to use all the options used here.
Most of the time the plugin's readme will have all the things needed in its installation section, So you could just copy and paste it in the M table, we will show it how you would do it in the next section. I would recommend you go and read look at the lazy.nvim documentation to learn more about it.

Heres how the simplest config in this config looks like which-key.lua is a good example in this config

local M = {
  "folke/which-key.nvim",
  commit = "5224c261825263f46f6771f1b644cae33cd06995",
  event = "VeryLazy",
}

function M.config()
  require("which-key").setup {}
end

return M

You can see that the general structuring of file is same, we just defined the plugin gave its commit hash, event and then called the setup function.

So now let's add a plugin to this config

Let's say you want to add a new plugin to this config Here's how your process could look like.

let's add neorg plugin, which is a plugin used for note-taking and organizing.
First we will make a file in lua/user called as neorg.lua and Make a function M and place everything that Neorg's Readme recommends.

local M = {
	"nvim-neorg/neorg",
    commit = "6aa9fd303c807ed1ca3fb15cdeab1e322d02fd31", -- we give it a commit hash ourself
	ft = "norg",
	cmd = "Neorg",
	build = ":Neorg sync-parsers",
	opts = {
		load = {
			["core.defaults"] = {}, -- Loads default behaviour
			["core.export"] = {},
			["core.concealer"] = {}, -- Adds pretty icons to your documents
			["core.dirman"] = { -- Manages Neorg workspaces
				config = {
					workspaces = {
						notes = "~/notes",
					},
				},
			},
		},
	},
	dependencies = { { "nvim-lua/plenary.nvim" } },
}

return M

if you would Want to add setup function to add your own config you would add this bewteen M table and return M

function M.config()
    require('neorg').setup {
        load = {
            ["your.required.module"] = {},
            ["your.required.module"] = {
                config = {
                    some_option = true
                }
            }
        }
    }
end

After making the file when you will reopen neovim lazy will install the plugin and apply its config itself and voila you have added a new plugin to the config