使用 lua 設定 neovim (1)

Jul 18, 2022

對於 vim script 一直都很不熟悉,也感到很頭痛。

剛好最近在查 nvim-treesitter 的設定資料時,看到有很多人都已經將

neovim 的設定檔換成用 lua 寫,整個感覺乾淨了很多,也很容易閱讀,而且 lua 上手也蠻快的。

所以花了一些時間,參考了一些現成的設定檔包括

  • kabinspace/AstroVim
  • lvim-tech/lvim
  • crivotz/nv-ide
  • nvim-lua/kickstart.nvim
  • 最後決定從 AstroVim 來改,因為整個架構比較簡單,並且大部分設定的 Plugin 都是我有在用的。

    AstroVim 預設安裝的 Plugins

    AstroVim 的設計與優點

    AstroVim 的設計主打

    AstroVim should allow you to extend its functionality without going outside of the user directory!

    理論上要增加 plugin 或者修改設定,只需要在 /lua/user/ 底下增加設定檔覆蓋預設的設定值。

    舉例來說,想要增加 nvim-treesitter 支援的語法,只需要在 /lua/user/settings.lua

    增加 nvim-treesitter 的設定即可。

    local config = { ... overrides = { treesitter = { ensure_installed = { "html", "css", "vim", "lua", "javascript", "typescript", "jsonc", "json", "toml", "python", "cpp", "c", "bash", "dart", "go", "make", "ninja", "rust", }, }, }, ... }

    不需要去修改 /lua/configs/treesitter.lua 的設定。

    /lua/configs 底下的設定只用來初始化 Plugin 的,剩下的到 /lua/user 去客製化。

    這樣的好處是,之後可以很輕易的 merge AstroVim 的更新,不容易產生 conflict。

    AstroVim 的資料夾結構

    . ├── colors │ └── onedark.vim ├── init.lua ├── LICENSE └── lua ├── configs │ ├── autopairs.lua │ ├── bufferline.lua │ ├── cmp.lua │ ├── colorizer.lua │ ├── comment.lua │ ├── copilot.lua │ ├── gitsigns.lua │ ├── icons.lua │ ├── indent-line.lua │ ├── lsp │ │ ├── handlers.lua │ │ ├── init.lua │ │ ├── lsp-installer.lua │ │ ├── lspsaga.lua │ │ └── server-settings │ │ ├── jsonls.lua │ │ ├── pyright.lua │ │ ├── rust_analyzer.lua │ │ └── sumneko_lua.lua │ ├── lualine.lua │ ├── nvim-dap.lua │ ├── nvim-tree.lua │ ├── symbols-outline.lua │ ├── telescope.lua │ ├── toggleterm.lua │ ├── treesitter.lua │ └── which-key.lua ├── core │ ├── autocmds.lua │ ├── defaults.lua │ ├── mappings.lua │ ├── options.lua │ ├── plugins.lua │ ├── status.lua │ └── utils.lua ├── onedark │ ├── base.lua │ ├── colors.lua │ ├── init.lua │ ├── lsp.lua │ ├── others.lua │ ├── treesitter.lua │ └── util.lua ├── packer_compiled.lua └── user ├── null-ls.lua ├── server-settings │ └── pylsp.lua └── settings.lua

    init.lua

    neovim 設定檔的起始點是 init.lua

    local utils = require "core.utils" -- 關閉不需要的功能 -- 例如 g:loaded_gzip 等,加快啟動速度 utils.disabled_builtins() -- 下載 packer.nvim 來管理 plugins utils.bootstrap() -- 載入 impatient.nvim 用於加速 lua modules 的載入 utils.impatient() local sources = { "core.options", "core.autocmds", "core.plugins", "core.mappings", } -- 依照 sources 的順序,載入 lua 設定檔 for _, source in ipairs(sources) do local status_ok, fault = pcall(require, source) if not status_ok then error("Failed to load " .. source .. "\n\n" .. fault) end end -- 載入 user settings local config = utils.user_settings() if type(config.polish) == "function" then config.polish() else error "The polish value in your user configuration must be a function" end -- keep this 關閉

    客製化

    關閉 AstroVim 預設的 Plugin

    AstroVim 有在 /lua/user/settings.lua 提供 enabled 的設定

    enabled = { bufferline = true, nvim_tree = true, . . . which_key = true, ts_rainbow = true, ts_autotag = true, nvim_dap = true }

    增加新的 plugin

    github/copilot.vim 為例子在 /lua/user/settings.lua 增加

    plugins = { { "github/copilot.vim", config = function() require("user.configs.copilot").config() end, }, },

    並將 copilot 的設定檔放在 /lua/user/configs/copilot.lua

    local M = {} function M.config() end vim.g.copilot_no_tab_map = true vim.g.copilot_filetypes = { cpp = false, h = false, cc = false, } return M

    這樣就在不修改 AstroVim 的預設設定檔的情況下,增加一個新的 Plugin。

    Reference

    GitHub - AstroNvim/AstroNvim: AstroNvim is an aesthetic and feature-rich neovim config that is extensible and easy to use with a great set of plugins
    AstroNvim is an aesthetic and feature-rich neovim config that is extensible and easy to use with a great set of plugins - GitHub - AstroNvim/AstroNvim: AstroNvim is an aesthetic and feature-rich neovim config that is extensible and easy to use with a great set of plugins
    favicon
    https://github.com/kabinspace/AstroVim
    GitHub - AstroNvim/AstroNvim: AstroNvim is an aesthetic and feature-rich neovim config that is extensible and easy to use with a great set of plugins
    ← Go home