Neovim のプラグインマネージャー lazy.nvim をインストールしてプラグインを入れる

vimlua

lazy.nvim は 2022年の11月にリリースされた nvim のプラグインマネージャー。高速に nvim が起動しプラグインは自動でインストールされる。

次のスクリプトで lazy.nvim をインストールする。 Lua のパッケージマネージャー luarocks が必要。

$ brew install luarocks
$ cat ~/.config/nvim/init.lua
-- Bootstrap lazy.nvim
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
vim.opt.rtp:prepend(lazypath)

-- Make sure to setup `mapleader` and `maplocalleader` before
-- loading lazy.nvim so that mappings are correct.
-- This is also a good place to setup other settings (vim.opt)
vim.g.mapleader = " "
vim.g.maplocalleader = "\\"

-- Setup lazy.nvim
require("lazy").setup({
  spec = {
    -- add your plugins here
  },
  -- Configure any other settings here. See the documentation for more details.
  -- colorscheme that will be used when installing plugins.
  install = { colorscheme = { "habamax" } },
  -- automatically check for plugin updates
  checker = { enabled = true },
})

nvim を起動するとスクリプトが実行されてリポジトリが clone される。

# :exec 'echo stdpath("data")'
***/.local/share/nvim

# !ls ***/.local/share/nvim
CHANGELOG.md    README.md       bootstrap.lua   lua             scripts         stylua.toml     vim.toml
LICENSE         TODO.md         doc             manifest        selene.toml     tests                                                                                                 

問題なく動いているか確認する。

# :checkhealth lazy
==============================================================================
lazy: require("lazy.health").check()

lazy.nvim ~
- {lazy.nvim} version `11.14.1`
- OK {git} `version 2.39.2 (Apple Git-143)`
- OK no existing packages found by other package managers
- OK packer_compiled.lua not found

luarocks ~
- checking `luarocks` installation
- OK no plugins require `luarocks`, so you can ignore any warnings below
- OK {luarocks} `/opt/homebrew/bin/luarocks 3.11.1`
- WARNING `lua` version `5.1` needed, but found `Lua 5.4.7  Copyright (C) 1994-2024 Lua.org, PUC-Rio`
- WARNING {lua5.1} or {lua} or {lua-5.1} version `5.1` not installed

:Lazy で UI が開く。

fuzzy finder である telescope.nvim を入れる。 直接書くか ~/.config/nvim/lua/plugins ディレクトリの lua ファイルに書いて import する。 live grep のために ripgrep もインストールする。

spec = {
  {
    'nvim-telescope/telescope.nvim', tag = '0.1.8',
    dependencies = { 'nvim-lua/plenary.nvim' } -- & brew install ripgrep for live grep
  }
  -- { import = "plugins" },
},

:Telescope find_files でプレビューと共にファイルを検索して開ける。C-t で新しいタブで開く。

キーマップを設定する。

vim.g.mapleader = " "

local builtin = require('telescope.builtin')
vim.keymap.set('n', '<leader>ff', builtin.find_files, { desc = 'Telescope find files' })
vim.keymap.set('n', '<leader>fg', builtin.live_grep, { desc = 'Telescope live grep' })

colorscheme を設定したり lualine.nvim を入れてモードなどが下に表示されるようにしたり、 neo-tree.nvim で File explorer を表示したり、Copilot がはたらくようにする。

{
  { 
    "EdenEast/nightfox.nvim",
    config = function()
      vim.cmd("colorscheme carbonfox")      
    end
  },
  {
    'nvim-lualine/lualine.nvim',
    dependencies = { 'nvim-tree/nvim-web-devicons' },
    config = function()
    ...
    end
  },
  {
    'github/copilot.vim',
    -- :Copilot setup
  }
  {
    "nvim-neo-tree/neo-tree.nvim",
    branch = "v3.x",
    dependencies = {
      "nvim-lua/plenary.nvim",
      "nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
      "MunifTanjim/nui.nvim",
      -- "3rd/image.nvim", -- Optional image support in preview window: See `# Preview Mode` for more information
    }
    -- :Neotree
  }
}

参考

プラグインマネージャーの歴史と新世代のプラグインマネージャー dpp.vim

Neovim環境構築とおすすめプラグイン - SunLTechLogs