如何用 lazy.nvim 的 performance.rtp 配置重置运行时路径并禁用内置插件以加快启动?
【免费下载链接】lazy.nvim💤 A modern plugin manager for Neovim项目地址: https://gitcode.com/GitHub_Trending/la/lazy.nvim
如果你的 Neovim 启动偏慢,而你希望保留 lazy.nvim 的插件管理能力,同时减少启动阶段加载的运行时目录和 Vim 内置插件,lazy.nvim 提供了performance.rtp这一组配置项来完成这件事:重置运行时路径(runtime path)、可选地追加自定义路径、以及按名字禁用指定内置插件。本文基于 lazy.nvim 的官方文档 doc/lazy.nvim.txt 和 lua/lazy/core/config.lua 的实际实现,说明这些配置放在哪里、每个字段的含义,以及如何用内置的 profiler 核对加载耗时。
performance.rtp 各字段的作用
官方帮助文档的完整配置参考中,performance一节长这样(摘自 doc/lazy.nvim.txt):
performance = { cache = { enabled = true, }, reset_packpath = true, -- reset the package path to improve startup time rtp = { reset = true, -- reset the runtime path to $VIMRUNTIME and your config directory ---@type string[] paths = {}, -- add any custom paths here that you want to includes in the rtp ---@type string[] list any plugins you want to disable here disabled_plugins = { -- "gzip", -- "matchit", -- "matchparen", -- "netrwPlugin", -- "tarPlugin", -- "tohtml", -- "tutor", -- "zipPlugin", }, }, },各字段含义以文档注释为准:
reset = true:把运行时路径重置为$VIMRUNTIME和你的配置目录,避免 rtp 里积累其他来源的路径。paths = {}:字符串数组,用于把自定义路径追加进 rtp,文档说明为 “add any custom paths here that you want to includes in the rtp”。disabled_plugins = {}:字符串数组,列出你要禁用的内置插件名;文档以gzip、matchit、matchparen、netrwPlugin、tarPlugin、tohtml、tutor、zipPlugin等作为示例条目。reset_packpath = true:重置 package path,注释明确写其目的是 “reset the package path to improve startup time”。cache.enabled = true:启用缓存。参考配置中以上默认值都是开启状态,即这些行为在默认配置下已经生效。
理解这条优化路径的前提:文档在 Startup Sequence 一节说明,lazy.nvim 不使用 Neovim 的 package 机制,而是完全禁用插件加载(vim.go.loadplugins = false),接管整个启动流程。对应地,lua/lazy/core/config.lua 中可以看到,当performance.rtp.reset为真时,vim.opt.rtp会被重设为固定的几个条目(配置目录、data/site、lazy.nvim 自身、$VIMRUNTIME、nvim 的 lib 目录、配置目录的after),随后performance.rtp.paths中的路径逐个 append 进 rtp。也就是说reset决定“保留哪些路径”,paths决定“额外加入哪些路径”。
在配置中启用并调整 performance.rtp
配置写在你的require("lazy").setup({...})调用中,也就是~/.config/nvim/lua/config.lua里(文档的 Single File Setup 示例中该调用位于此文件)。文档提示 “Configure any other settings here”,performance属于其中之一:
-- ~/.config/nvim/lua/config.lua 中的 setup 调用 require("lazy").setup({ spec = { -- 你的插件列表 }, performance = { cache = { enabled = true, }, reset_packpath = true, -- reset the package path to improve startup time rtp = { reset = true, -- reset the runtime path to $VIMRUNTIME and your config directory paths = {}, -- 需要额外加入 rtp 的自定义路径 disabled_plugins = { "netrwPlugin", -- 要禁用的内置插件名 "zipPlugin", }, }, }, })上面的disabled_plugins示例值直接取自文档参考配置中的示例条目,你可以按自己环境里实际想禁用的内置插件增删。如果你没有想禁用的内置插件,保持disabled_plugins = {}即可;reset与reset_packpath保持true就是参考配置中的默认状态。
关于“禁用”的实际行为,源码侧的对应逻辑在 lua/lazy/core/loader.lua 的M.setup():它遍历performance.rtp.disabled_plugins中的每一项并调用disable_rtp_plugin(),把插件名记入内部禁用集合。注意它针对的是 Vim/Neovim 自带的内置插件(即文档示例里那些*Plugin名),而不是你在spec中管理的 lazy.nvim 插件——禁用由 lazy.nvim 管理的插件要用插件 spec 里的enabled = false(文档迁移指南中disable=true ➡️ enabled = false的对应关系)。
核对启动性能
配置生效后的核对手段,文档给出的是 lazy.nvim 自带的 profiler:
:Lazy profile:Lazy profile打开 profiling 视图,展示每个插件为什么加载、加载花了多久。文档说明 startup 阶段在VimEnter或BufReadPre之前用到的 Lua 文件会被字节编译并缓存(类似 impatient.nvim 的做法),cache.enabled = true即对应这一缓存能力。
如果只是想调试 lazy.nvim 自身的加载开销,参考配置还提供profiling一节(profiling.loader和profiling.require,默认均为false),文档明确警告这会带来额外开销,只在调试 lazy.nvim 时开启:
profiling = { -- Enables extra stats on the debug tab related to the loader cache. loader = false, -- Track each new require in the Lazy profiling tab require = false, },文档作者给出的11ms加载时间(93 个插件)仅是其个人配置的示例数字,不要把它当作你可以达到的固定预期;以你自己:Lazy profile视图中的实际数据为准。
限制与注意事项
reset = true会重建 rtp,文档注释表述为 “$VIMRUNTIME and your config directory”;从 lua/lazy/core/config.lua 的实现看,实际保留的路径还包括data/site、lazy.nvim 自身目录和 nvim 的 lib 目录。如果你原来依赖 rtp 中其他来源的路径,需要在performance.rtp.paths中显式补回。disabled_plugins只作用于内置插件;对 lazy.nvim 管理的插件无效。- 这些配置在参考配置中默认已开启(
reset、reset_packpath、cache.enabled均为true)。如果你之前显式关闭过其中任何一项,把它改回true即可恢复文档参考配置中的行为,无需其他步骤。 - 文档未给出任何固定的启动耗时阈值或成功判定数值;是否“加快启动”应通过
:Lazy profile前后对比来判断。
【免费下载链接】lazy.nvim💤 A modern plugin manager for Neovim项目地址: https://gitcode.com/GitHub_Trending/la/lazy.nvim
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考