Try   HackMD

[lazyvim] lazyvim 學習筆記

tags: 開發環境 lazyvim

created: 2025/03/14

runtimepath

The runtimepath is a group of directories that Neovim will search inside of when it starts up for these additional configuration files. They can be directories internal to Neovim, but can also be created by the user too.
What is the runtimepath?

Neovim 會讀取這些路徑檔案 => 這些檔案可以是設定檔或者 clone 下來的套件。

如何查看目前的 Neovim 的 runtimepath 有哪些?

打開 Neovim 後可以執行 :echo nvim_list_runtime_paths()

以最初始的(未有任何的設定檔及套件)應該可以得到下列的結果:

['/Users/liuweilun/.config/nvim', '/opt/homebrew/Cellar/neovim/0.10.4_1/share/nvim/runtime', '/opt/homebrew/Cellar/neovim/0.10.4
_1/share/nvim/runtime/pack/dist/opt/matchit', '/opt/homebrew/Cellar/neovim/0.10.4_1/lib/nvim']

也可以再打開 Neovim 後執行 :h runtimepath

可以看到 Neovim 搜尋不同的目錄(directories),其中第一個 $XDG_CONFIG_HOME/nvim 這個就是指向 ~/.config/nvim/ 目錄(direactory)。

lua/ 供 Neovim 讀取模組的 wrapper directory

  • Neovim 會自動抓取 runtimepath 中最上層的 lua/ 當作模組的入口
  • 在引入模組時不需額外再寫 lua/ 路徑

以下列的資料夾結構範例:

~/.config/nvim
├── lua
│   ├── config
│   │   ├── lazy.lua
└── init.lua

~/.config/nvim/init.lua 中寫

-- require 可以想像成 JavaScript 中的 import -- 不需要寫 required("lua.config.lazy") require("config.lazy")

Neovim 就會讀取 lua/config/lazy.lua

File Structure

~/.config/nvim
├── lua
│   ├── config
│   │   ├── autocmds.lua
│   │   ├── keymaps.lua
│   │   ├── lazy.lua
│   │   └── options.lua
│   └── plugins
│       ├── spec1.lua
│       ├── **
│       └── spec2.lua
└── init.lua

init.lua

Neovim 讀取設定的入口,並用 lua/ 當作 wrapper directory:

-- ~/.config/nvim/init.lua -- bootstrap lazy.nvim, LazyVim and your plugins require("config.lazy")

lazy.lua 是主要的設定檔案

starter template:

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim" if not (vim.uv or vim.loop).fs_stat(lazypath) then local lazyrepo = "https://github.com/folke/lazy.nvim.git" local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath }) if vim.v.shell_error ~= 0 then vim.api.nvim_echo({ { "Failed to clone lazy.nvim:\n", "ErrorMsg" }, { out, "WarningMsg" }, { "\nPress any key to exit..." }, }, true, {}) vim.fn.getchar() os.exit(1) end end -- 將 lazyvim 放入 runtimepath 中 vim.opt.rtp:prepend(lazypath) require("lazy").setup({ spec = { -- add LazyVim and import its plugins { "LazyVim/LazyVim", import = "lazyvim.plugins" }, -- import/override with your plugins -- 會讀取 `~/.config/nvim/lua/config/plugins` 這個路徑的 lua files { import = "plugins" }, }, defaults = { -- By default, only LazyVim plugins will be lazy-loaded. Your custom plugins will load during startup. -- If you know what you're doing, you can set this to `true` to have all your custom plugins lazy-loaded by default. lazy = false, -- It's recommended to leave version=false for now, since a lot the plugin that support versioning, -- have outdated releases, which may break your Neovim install. version = false, -- always use the latest git commit -- version = "*", -- try installing the latest stable version for plugins that support semver }, install = { colorscheme = { "tokyonight", "habamax" } }, checker = { enabled = true, -- check for plugin updates periodically notify = false, -- notify on update }, -- automatically check for plugin updates performance = { rtp = { -- disable some rtp plugins disabled_plugins = { "gzip", -- "matchit", -- "matchparen", -- "netrwPlugin", "tarPlugin", "tohtml", "tutor", "zipPlugin", }, }, }, })

因為有把 lazyvim 放入 runtimepath 中:

lazy.nvim explained (04:19 - 06:05)

vim.opt.rtp:prepend(lazypath)

所以在此時執行 :echo nvim_list_runtime_paths() 會發現 lazyvim 以及其他有使用的套件都放入 runtimepath 了。

['/Users/liuweilun/.config/nvim', '/Users/liuweilun/.local/share/nvim/lazy/lazy.nvim', '還有其他用到的套件...']

options.lua 是 vim 的基本設定檔案

plugins

snacks.nvim Explorer

可以參考這篇的配置

Neovim: Extend snacks.nvim Explorer

return {
  {
    "folke/snacks.nvim",
    priority = 1000,
    lazy = false,
    opts = {
      picker = {
        enabled = true,
        sources = {
          explorer = {
            auto_close = true,
            hidden = true,
            layout = {
              preset = "default",
              preview = false,
            },
          },
        },
      },
    },
  },
}

formatting