summaryrefslogtreecommitdiffstats
path: root/docs/manual/configuring/lazy-loading/overview.md
blob: cc7a4ea8be00bae626486327e982c2e63efac7f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Overview {#sec-lazy-overview}

Lazy loading defers plugin initialization until the plugin is actually needed.
Rather than loading every plugin at startup, nvf (via its `lz.n` backend) loads
plugins on demand, triggered by commands, events, filetypes, or keymaps. This
reduces startup time, especially in configurations with many plugins.

## Why Should I LazyLoad? {#sec-why-get-lazy}

Neovim evaluates all sourced Lua and Vimscript at startup. Plugins that register
autocommands, define highlight groups, or call `require()` unconditionally all
contribute to startup latency. For frequently opened editors (terminals,
`git commit`, quick file edits) even a few hundred milliseconds is noticeable.
Lazy loading moves that cost to the first use of a feature instead of every
startup.

## lz.n {#sec-lazy-backend}

[lz.n]: https://github.com/lumen-oss/lz.n

nvf uses [lz.n] as its lazy-loading backend. `lz.n` is a minimal, declarative
lazy loader for Neovim that integrates with Nix-managed plugin paths. Unlike
runtime package managers, `lz.n` does not download or manage plugins; it only
controls when they are sourced. This plays nicely with our model of using Nix to
manage plugins.

## Enabling Lazy Loading {#sec-lazy-enable}

Lazy loading is configured via `vim.lazy.plugins`, which accepts an attribute
set mapping plugin names to lazy-loading specifications:

```nix
{
  config.vim.lazy.plugins = {
    "my-plugin.nvim" = {
      package = pkgs.vimPlugins.my-plugin-nvim;

      # Mark the plugin as lazy. It will not load at startup.
      lazy = true;

      # Define at least one trigger to load the plugin.
      cmd = [ "MyPluginCommand" ];
    };
  };
}
```

If no trigger (`cmd`, `event`, `keys`, `ft`) is specified alongside
`lazy =
true`, the plugin will never be automatically loaded. You must call
`require("lz.n").load("my-plugin.nvim")` manually or define a trigger.