LazyVimでは、lazy.nvimを使ってプラグインのインストール、更新、依存関係、遅延読み込みを管理します。ユーザー設定は通常lua/plugins/へ追加します。
1. プラグイン仕様の場所#
代表的なディレクトリ構造は次のとおりです。
~/.config/nvim/
├── init.lua
├── lazy-lock.json
├── lua/
│ ├── config/
│ │ ├── lazy.lua
│ │ ├── options.lua
│ │ ├── keymaps.lua
│ │ └── autocmds.lua
│ └── plugins/
│ ├── telescope.lua
│ └── nvim-tree.lua
└── stylua.tomllua/plugins/に置いたLuaモジュールは、通常lua/config/lazy.luaの{ import = "plugins" }によって自動的に検出されます。
2. Telescopeを追加する#
lua/plugins/telescope.luaを作成し、次のように書きます。
return {
{
"nvim-telescope/telescope.nvim",
dependencies = { "nvim-lua/plenary.nvim" },
config = function()
require("telescope").setup({
defaults = {
layout_strategy = "horizontal",
sorting_strategy = "ascending",
},
pickers = {
find_files = {
hidden = true,
},
},
})
end,
keys = {
{ "<leader>ff", "<cmd>Telescope find_files<cr>", desc = "Find files" },
{ "<leader>fg", "<cmd>Telescope live_grep<cr>", desc = "Live grep" },
},
cmd = { "Telescope" },
},
}主なフィールドは次のとおりです。
| フィールド | 役割 |
|---|---|
| プラグイン名 | Gitリポジトリを指定する |
dependencies | 依存プラグインを指定する |
config | 読み込み後の初期設定を行う |
keys | キー入力でプラグインを遅延読み込みする |
cmd | Exコマンドでプラグインを読み込む |
event | Neovimイベントで読み込む |
lazy = false | 起動時に読み込む |
3. プラグインを同期する#
設定ファイルを保存した後、Neovimで次を実行します。
:Lazy画面からSync、Install、Updateなどを選ぶか、Neovimを再起動します。プラグインが読み込まれたかは、:Lazyの状態表示や、指定したキーマップ・コマンドで確認できます。
4. nvim-treeの例#
return {
{
"nvim-tree/nvim-tree.lua",
dependencies = { "nvim-tree/nvim-web-devicons" },
config = function()
require("nvim-tree").setup({
view = { width = 30 },
})
end,
keys = {
{ "<leader>e", "<cmd>NvimTreeToggle<cr>", desc = "Toggle file tree" },
},
},
}<leader>eを押したときにnvim-treeが読み込まれ、ファイルツリーが開きます。別の設定ファイルへ同じプラグインを手動でimportする必要は、通常ありません。
5. 遅延読み込みの注意点#
- 起動時にすぐ必要なオプションは、プラグインの
initまたは適切な設定ファイルへ置く。 - プラグインのAPIを呼ぶ設定は、
configの中へ置く。 - プラグインのコマンドやキーは、
cmdやkeysで遅延読み込みを設定する。 lua/config/plugins.luaを使う場合は、lazy.luaから明示的にimportされているか確認する。
まとめ#
- プラグイン仕様は
lua/plugins/へ追加する。 dependencies、config、keys、cmd、eventで読み込み方を指定する。- 多くの場合、手動でプラグインを読み込む必要はない。
:Lazyでインストールと更新、状態確認を行う。

