如何用 lazy.nvim 的 performance.rtp 配置重置运行时路径并禁用内置插件以加快启动?

📅 发布时间:2026/9/14 5:26:45
如何用 lazy.nvim 的 performance.rtp 配置重置运行时路径并禁用内置插件以加快启动?
如何用 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.txtperformance { 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文档迁移指南中disabletrue ➡️ 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),仅供参考