Examples of lazyloading

This is a list of examples of how to lazyload plugins.

The examples use packer as their plugin manager.

Table of contents

simple lazyloading examples

use{'nvim-telescope/telescope.nvim',cmd='Telescope'}
use{'tommcdo/vim-lion',keys={{'x','gl'},{'n','gl'},{'x','gL'},{'n','gL'}}}

complex lazyloading examples

use{'hrsh7th/nvim-cmp',requires={
    {'hrsh7th/cmp-buffer',after='nvim-cmp'}
}}
use{'glepnir/dashboard-nvim',
    cmd={'Dashboard','DashboardNewFile'},
    setup=function ()
        vim.api.nvim_create_autocmd('Vimenter',{callback=function()
            if vim.fn.argc()==0 and vim.fn.line2byte('$')==-1 then
                vim.cmd'Dashboard'
            end
        end})
    end
}
use{'anuvyklack/pretty-fold.nvim',
    requires='anuvyklack/nvim-keymap-amend',
    config='require "pretty-fold".setup{}',
    event='User isfolded',
    setup=function ()
        local fn=vim.fn
        local fastfoldtimer
        fastfoldtimer=fn.timer_start(2000,function()
            if #fn.filter(fn.range(1,fn.line'$'),'foldlevel(v:val)>0')>0 then
                vim.cmd('doautocmd User isfolded')
                fn.timer_stop(fastfoldtimer)
            end
        end,{['repeat']=-1})
    end
}
use{'elihunter173/dirbuf.nvim',
    cmd={'Dirbuf'},
    setup=function()
        vim.api.nvim_create_autocmd('BufEnter',{
            command="if isdirectory(expand('%')) && !&modified|execute 'Dirbuf'|endif"
        })
    end
}

requires lazyloading examples

use{'rbong/vim-flog',
    setup=function ()
        vim.api.nvim_create_user_command(cmd,[[
        lua require"packer.load"({"vim-fugitive"},{},_G.packer_plugins)
        require"packer.load"({"vim-flog"},{cmd="Flog",l1=<line1>,l2=<line2>,bang=<q-bang>,args=<q-args>,mods="<mods>"},_G.packer_plugins)]],
            {nargs='*',range=true,bang=true,complete='file'}
        )
    end,
    opt=true
}
use{'tpope/vim-fugitive',
    opt=true
}

rplugin lazyloading examples

vim.g.loaded_remote_plugins="not loaded"
use{'ripxorip/aerojump.nvim',
    config=function ()
        vim.cmd[[
        if g:loaded_remote_plugins=="not loaded"
            unlet g:loaded_remote_plugins
            source /usr/share/nvim/runtime/plugin/rplugin.vim
        endif
        ]]
    end,
    cmd='Aerojump'
}

builtin lazyloading examples

vim.g.loaded_tutor_mode_plugin=1
vim.api.nvim_create_user_command('Tutor',[[
delcommand Tutor
unlet g:loaded_tutor_mode_plugin
source /usr/share/nvim/runtime/plugin/tutor.vim
Tutor <args>]],{nargs='?',complete='custom,tutor#TutorCmdComplete'})
2 Likes

Hey!
Thank you very much for this really interesting post. I tried to implement one of your lazy loading technique for telescope:

use {
    "nvim-telescope/telescope.nvim",
    setup = function()
        require("plugins.telescope").config()
    end,
    requires = {
        {
            "nvim-telescope/telescope-fzf-native.nvim",
            run = "make"
        }
    },
    cmd = "Telescope"
}

Here is my problem. My telescope file is correctly required before telescope is loaded. This file also load my mapping :

vim.keymap.set("n", "<leader>f",
    function()
        if not pcall(builtin.git_files, ivy) then
            builtin.find_files(ivy)
        end
    end,
    builtin.opts
)

So the Telescope command is never executed in my mappings. Should I change the mapping the execute the Telescope command or is there a way to lazy load even thought I’m using a function instead of a command to open telescope…
I’m not sure that this question was really clear. Thank you anyway for this post (:

What I would recommend is replacing setup with config and adding keys={{"n","<leader>f"}},. (It is how I have it in my config)

Thou you could also add module="telescope", and only have local builtin=require "telescope.builtin" inside the mapping.

1 Like

I used the keys method and it worked just find, ty & have a good day!