Different ways to configure plugins through lazy vim what do they mean and which one do I use?

Hi everyone,

I’m really new to the whole neovim thing and I’m figuring out my way to setup a good environment for my programming needs. I’m following this awesom youtube tutorial by chris@machine I’m using lazy.nvim as my plugin manager and when trying to install nvim-cmp I found that I need to configure it and when searching on how. there where multiple ways :

  • editing the opts spec in lazy.nvim
return 
{
    "hrsh7th/nvim-cmp",
    opts = function(_, opts)
         -- configurations
    end,
}

what does opts = function(_, opts) mean, and how it’s actually called?

  • editing the config spec in lazy.nvim
{
    "hrsh7th/nvim-cmp",
    config = function()
         -- configurations
    end,
}

what is the different between config and opts ?

  • the last one was by chris@machine on his repo nvim-basic-ide which is an update one from the one he is using in the tutorial, but basically his way is like that
local M = {
  "hrsh7th/nvim-cmp",
}

function M.config()
  -- configurations
end

return M

like what M.config() means? when it will get executed ?

I read the lazy.nvim github docs about opts and config but I didn’t fully understand everything and I feel they are the same thing and I tried to search online but I didn’t find any thing useful?

I’m just trying to understand what I’m doing and not just copy pasting from online dotfiles

sorry for the long post

2 Likes

Here is a my current configurations

For the first and second questions you should read the lazy.nvim documentation and perhaps how it is useful in LazyVim

For the third question it is equivalent to M.config = function(.... you can then see it is equivalent to the second questions example

I read the lazy vim documentation for otps and config and didn’t get much, but, let me tell you what I understood :

  • First for opts spec
    the docs says it can be a table or fun(LazyPlugin, opts:table) what the parameters of this function means and how it will be executed. then it says the opts will be passed to plugin.config() although in the config spec docs it sayes the default will run require"MAIN".setup(opts)

  • Second the config spec
    the doces says it can be a table or fun(LazyPlugin, opts:table) again same issue what this function means and what the parameters means how should I implement it and how it will works.

I really appreaciate If you could help?!!!

I read the lazy vim documentation for otps and config and didn’t get much, but, let me tell you what I understood :

  • First for opts spec
    the docs says it can be a table or fun(LazyPlugin, opts:table) what the parameters of this function means and how it will be executed. then it says the opts will be passed to plugin.config() although in the config spec docs it sayes the default will run require"MAIN".setup(opts)

  • Second the config spec
    the doces says it can be a table or fun(LazyPlugin, opts:table) again same issue what this function means and what the parameters means how should I implement it and how it will works.

@indianboy42 I really appreaciate If you could help?!!!

Hi eastking, I’ll try to help, having recently spent time setting up lazy.nvim myself.

When you set up a bunch of plugins in lazy, all you are doing is specifying a big table of tables. It is declarative, not imperative. Thus you are rightly asking “when will it get executed?”

Once all the plugins are specified, lazy will (I presume) determine some order in which to load them. Some plugins might be given a higher priority by the user (folke recommends to give the colorscheme a high priority, for instance). Some plugins are dependent on others, so the dependencies are loaded first. Some plugins will never be loaded because the trigger never occurs. For example, I have ‘vimtex’ set to load only on ft=tex.

When a plugin is ‘loaded’, what occurs?

Well, most plugins need setup to be called. Take mini.surround as a simple example. If you simply specify ‘echasnovski/mini.surround’ and that’s it, nothing will happen. You need the following code to be run: require('echasnovski/mini.surround').setup() in order to activate the plugin. If you want to provide additional configuration (say, to disable a mapping), then you provide options to the setup function, like require('echasnovski/mini.surround').setup({ mappings = { find = '', find_left = '' } }).

OK, so how do we make this code get run in lazy.nvim?

One way is:

config = function(_, _)  -- I am not using either parameter
  require('echasnovski/mini.surround').setup({ mappings = { find = '', find_left = '' } })
}

Sweet. But a shorter way is to do this:

opts = {
  mappings = { find = '', find_left = '' }
}

Lazy will see that you have provided options and that will trigger it to call setup for you with those options.

What if you don’t want to change any mappings? Here are your choices. They are equivalent.

-- Choice 1
{
  'echasnovski/mini.surround',
  opts = {}
}

-- Choice 2
{
  'echasnovski/mini.surround',
  config = true
}

Both of these are more convenient than

{
  'echasnovski/mini.surround',
  config = function()
    require('mini.surround').setup()
  end
}

especially when you have a lot of plugins.

Lazy is just helping you out by optimising the common path. But it is only easy to understand this if you already know the background. I would like to have been able to read this kind of material in the docs when I was starting out.

While I’m here, I’ll mention the init function. I like to use that for keymaps.

  {
    "Pocco81/auto-save.nvim",
    init = function()
      require('which-key').register({
        t = {
          a = { ':ASToggle<CR>', 'Autosave' },
        }
      }, { prefix = '<leader>' })
    end
  },

(Note that the auto-save plugin does not require setup to be called, hence I do not have config = true in the spec.)

5 Likes

first thank you very much that was a very detailed comment, a couple of notes I have :

  • opts and config are equal and anyone of them can be used to configure the plugin, is that correct?

what are the parameters mean and how can I use them, because in the docs the config or opts can be table for function(LazyPlugin, opts:table) and I don’t understand the what are these parameters and how can I use them and what is the difference between table and function(LazyPlugin, opts:table)

opts is a table, config is a function (or true, which implies a basic default function . So no, they’re not equa, but they’re closely related.

The second argument to config is ‘opts’. The first is…idk. The docs mystify me as well.

2 Likes

Thank you I spent the last 2 hours discovering this (I am very new to the world of vim). Reading your answer and getting the confirmation feels like now I can stop haha.

FWIW, the first argument seems to be a Lua table of information about the plugin being configured. I also haven’t found any examples of that being used, however.

This gives some clues:

    config = function(lazyPlugin, _)
      vim.print({lazyPlugin})
    end