Neovim 用 Lua 配置
Vim 用 Vimscript(.vimrc)。Neovim 0.5+ 用 Lua(init.lua),更快、更现代、更适合写复杂逻辑。
配置文件位置:
~/.config/nvim/init.lua (主入口)
~/.config/nvim/lua/<modulename>.lua (子模块)
最小 init.lua
-- 基本选项
vim.opt.number = true
vim.opt.relativenumber = true
vim.opt.expandtab = true
vim.opt.tabstop = 4
vim.opt.shiftwidth = 4
vim.opt.smartindent = true
vim.opt.termguicolors = true
vim.opt.cursorline = true
vim.opt.scrolloff = 8
-- leader key
vim.g.mapleader = " "
vim.g.maplocalleader = ","
-- 快捷键
vim.keymap.set("n", "<leader>w", ":w<CR>")
vim.keymap.set("n", "<leader>q", ":q<CR>")
vim.keymap.set("v", "<", "<gv") -- 缩进后保持选区
vim.keymap.set("v", ">", ">gv")
vim.opt 设选项,vim.keymap.set 绑快捷键,vim.g 设全局变量。
4 个核心 API
| 用途 | |
|---|---|
vim.opt |
选项(number、expandtab) |
vim.g |
全局变量(mapleader、插件配置) |
vim.keymap.set(mode, key, action) |
快捷键 |
vim.api.nvim_* |
底层 API(编辑 buffer、创建窗口…) |
插件管理:Lazy.nvim
lazy.nvim 是事实标准——按需加载、并行安装、UI 友好。
Bootstrap
-- init.lua 顶部
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system({
"git", "clone", "--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable",
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
require("lazy").setup({
-- 插件列表
})
装插件
require("lazy").setup({
-- 主题
{ "catppuccin/nvim", name = "catppuccin", priority = 1000,
config = function() vim.cmd.colorscheme("catppuccin") end },
-- 文件树
{ "nvim-tree/nvim-tree.lua", config = true },
-- 模糊搜索
{ "nvim-telescope/telescope.nvim",
dependencies = { "nvim-lua/plenary.nvim" },
keys = {
{ "<leader>ff", "<cmd>Telescope find_files<cr>" },
{ "<leader>fg", "<cmd>Telescope live_grep<cr>" },
}},
-- LSP
{ "neovim/nvim-lspconfig",
config = function()
require("lspconfig").lua_ls.setup({})
require("lspconfig").pyright.setup({})
end},
-- 自动补全
{ "hrsh7th/nvim-cmp",
dependencies = { "hrsh7th/cmp-nvim-lsp", "hrsh7th/cmp-buffer" }},
-- Git
{ "tpope/vim-fugitive", cmd = "Git" },
{ "lewis6991/gitsigns.nvim", config = true },
-- Treesitter
{ "nvim-treesitter/nvim-treesitter",
build = ":TSUpdate",
config = function()
require("nvim-treesitter.configs").setup({
ensure_installed = {"lua", "python", "javascript", "rust"},
highlight = { enable = true },
})
end},
})
打开 nvim → :Lazy 看安装进度 / 状态。
配置分模块
~/.config/nvim/
├── init.lua 入口
└── lua/
├── options.lua 选项
├── keymaps.lua 快捷键
├── plugins.lua 插件列表
└── plugins/
├── lsp.lua LSP 详细配置
└── telescope.lua
init.lua:
require("options")
require("keymaps")
require("plugins")
写自己的小命令
-- 自定义命令
vim.api.nvim_create_user_command("Greet", function(opts)
print("Hello, " .. (opts.args ~= "" and opts.args or "world"))
end, { nargs = "?" })
-- 使用::Greet 或 :Greet Alice
-- 自动命令
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*.lua",
callback = function()
vim.lsp.buf.format() -- 保存前格式化
end,
})
LSP(语言服务器)的最小连接
local lsp = require("lspconfig")
lsp.lua_ls.setup({
settings = {
Lua = {
diagnostics = { globals = {"vim"} },
}
}
})
vim.keymap.set("n", "gd", vim.lsp.buf.definition)
vim.keymap.set("n", "K", vim.lsp.buf.hover)
vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename)
需要装对应 LSP 服务器(brew install lua-language-server、pip install pyright 等)。
调试 Lua 配置
:lua print(vim.fn.stdpath("config")) -- 直接执行 Lua
:Lazy check -- 插件状态
:checkhealth -- 系统体检
:LspInfo -- 当前 buffer 的 LSP 状态
学习路径
- Learn Neovim Lua API
- LazyVim / NvChad / AstroNvim——预配置发行版,先用着摸熟,再渐进改自己的
- neovim-from-scratch 系列教程
→ 下一篇 Redis Lua 脚本