前言
使用lazy配置neovim还算简单。
但由于许多插件和neovim本体更新迅速,许多教程已经过时(由于是新更新的,ai酱也不知道捏)。
整理一下配置过程。
init.lua
在配置目录下创建init.lua。
理论上只需要一个文件就能配置完nvim(只要你不嫌维护麻烦>_<)
文件结构大概长这样
.
├── init.lua
└── lua├── config│ ├── autocmds.lua│ ├── keymaps.lua│ ├── lazy.lua│ ├── options.lua└── plugins├── cmp.lua├── copilot.lua├── formatter.lua├── lspconfig.lua├── nvim-tree.lua├── telescope.lua├── theme.lua├── toggleterm.lua└── treesitter.lua
init.lua里用require将其他文件加载进来
require("config.options")
require("config.keymaps")
require("config.autocmds")
require("config.lazy")require("nvim-treesitter").install { 'c', 'cpp','rust', 'javascript','lua','html','vim','vimdoc', 'xml', 'python', 'markdown','markdown_inline', 'json', 'bash', 'zsh'}
vim.lsp.enable('clangd')
vim.lsp.enable('pyright')
vim.lsp.enable('rust_analyzer')
options里写option,keymaps里写keymap,autocmds里写auto command。
然后就创建完成了。
lazy插件管理
用lazy处理插件下载、更新、配置,很方便。
启动后会自动从github下载lazy,你可能需要处理一些网络问题。
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) thenvim.fn.system({"git","clone","--filter=blob:none","https://github.com/folke/lazy.nvim.git","--branch=stable", -- latest stable releaselazypath,})
end
vim.opt.rtp:prepend(lazypath)
-- 加载插件
require("lazy").setup({-- 这里可以自动引入 lua/plugins 文件夹下的所有模块{ import = "plugins" },
})
其余插件
其余插件全部扔在plugins文件夹里,如果为空的话lazy不会启动。
主题
tokyonight。
大概这样写。config里写配置
return {"folke/tokyonight.nvim",lazy = false,priority = 1000,config = function()vim.cmd[[colorscheme tokyonight]] end,
}
treesitter
语法高亮插件。
如果未安装主题可能看不到配色。
使用require("nvim-treesitter").install开启高亮
return {'nvim-treesitter/nvim-treesitter',lazy = false,build = ':TSUpdate'
}
conform
代码格式化插件。
为每个语言配置格式化工具,记得在外部安装好。
return {{"stevearc/conform.nvim",config = function()local conform = require("conform")conform.setup({formatters_by_ft = {html = { "prettier" },json = { "prettier" },lua = { "stylua" },markdown = { "prettier" },python = { "isort", "black" },rust = { "rustfmt" },},})vim.keymap.set({ "n", "v" }, "<leader>=", function()conform.format({lsp_fallback = true,async = false,timeout_ms = 500,})end, { desc = "Format file or range (in visual mode)" })end,},
}
LSP
理论上neovim 0.11自带lsp客户端,只需要配置lspconfig。
nvim-lspconfig经历了一次不兼容更新,现有的github master分支为新分支。
安装好后记得在init.lua里enable
return {{"neovim/nvim-lspconfig",},
}
cmp
自动补全插件。
需要用vim.lsp.config设置capabilities。
return {{"hrsh7th/nvim-cmp",dependencies = {"hrsh7th/cmp-nvim-lsp","hrsh7th/cmp-buffer","hrsh7th/cmp-path","neovim/nvim-lspconfig",},config = function()local cmp = require("cmp")local caps = require('cmp_nvim_lsp').default_capabilities()vim.lsp.config('*', {capabilities = caps})cmp.setup({completion = {autocomplete = false },sources = cmp.config.sources({{ name = "nvim_lsp" }, -- LSP{ name = "path" }, -- 路径{ name = "buffer" }, -- 当前文件内容 (Buffer)}),mapping = cmp.mapping.preset.insert({['<C-n>'] = cmp.mapping.select_next_item(),['<C-p>'] = cmp.mapping.select_prev_item(),['<C-i>'] = cmp.mapping.complete(),['<C-e>'] = cmp.mapping.confirm({ select = true }), }),})end,}
}
参考文献
https://dev.to/slydragonn/ultimate-neovim-setup-guide-lazynvim-plugin-manager-23b7
https://www.mintimate.cn/2023/01/10/guideForLunarvim
https://youngxhui.top/2023/07/nvim-guideline-1basic-config
https://github.com/rockerBOO/awesome-neovim