配置 Neovim

配置 Neovim

本文记录从零开始配置 Neovim 的过程,主要内容包括:

  1. 基础选项配置
  2. 安装并配置插件管理器 lazy.nvim
  3. 常用界面与目录树插件(主题 tokyonight、状态栏 lualine、文件树 nvim-tree)
  4. 语法高亮与自动括号(nvim-treesitter + nvim-autopairs)以及配置文件语法检查(mason + nvim-lspconfig)

基础选项配置

首先创建核心配置目录,并编辑基础配置文件 ~/.config/nvim/lua/core/options.lua:

1
2
mkdir -p ~/.config/nvim/lua/core
nvim ~/.config/nvim/lua/core/options.lua

在其中配置行号、搜索、外观,以及将 Tab 键转换为空格而非制表符的缩进规则:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
local opt = vim.opt

-- 行号
opt.number = true

-- 缩进与制表符
opt.tabstop = 4
opt.shiftwidth = 4
opt.softtabstop = 4
opt.expandtab = true
opt.autoindent = true
opt.smartindent = true

-- 防止文本自动折行包裹
opt.wrap = false

-- 高亮当前光标所在行
opt.cursorline = true

-- 默认新分割窗口的位置
opt.splitright = true
opt.splitbelow = true

-- 搜索配置
opt.ignorecase = true
opt.smartcase = true

-- 外观
opt.termguicolors = true
opt.signcolumn = "yes"

提示:如果打开已有的历史文件,里面已经存在制表符(\t),可以在开启 expandtab = true 的情况下输入命令 :retab,即可将文件中现有的所有制表符一键替换为空格。

接着编辑入口文件 ~/.config/nvim/init.lua,引入刚刚编写的基础配置:

1
nvim ~/.config/nvim/init.lua
1
require("core.options")

配置插件管理器 (lazy.nvim)

这里选择使用 lazy.nvim 作为插件管理器。首先安装依赖的 luarocks 包管理器、Treesitter 编译语法树所需的 tree-sitter-cli,以及 Mason 自动下载安装配置文件检查服务所需的环境(nodejs、npm、unzip 等):

1
sudo pacman -S --needed luarocks tree-sitter-cli ripgrep fd nodejs npm unzip wget curl git

创建插件目录并编辑 ~/.config/nvim/lua/core/lazy.lua:

1
2
mkdir -p ~/.config/nvim/lua/plugins
nvim ~/.config/nvim/lua/core/lazy.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)

vim.g.mapleader = " "
vim.g.maplocalleader = "\\"

require("lazy").setup({
spec = {
{ import = "plugins" },
},
install = { colorscheme = { "tokyonight", "habamax" } },
checker = { enabled = true },
})

在 ~/.config/nvim/init.lua 中添加对 core.lazy 的引用:

1
2
require("core.options")
require("core.lazy")

常用插件配置

我们先按功能模块创建对应的插件配置文件:

1
2
3
4
5
touch ~/.config/nvim/lua/plugins/theme.lua
touch ~/.config/nvim/lua/plugins/statusbar.lua
touch ~/.config/nvim/lua/plugins/tree.lua
touch ~/.config/nvim/lua/plugins/syntax.lua
touch ~/.config/nvim/lua/plugins/lsp.lua

主题配色

这里选择 tokyonight.nvim,在 ~/.config/nvim/lua/plugins/theme.lua 中添加:

1
2
3
4
5
6
7
8
9
10
return {
{
"folke/tokyonight.nvim",
lazy = false,
priority = 1000,
config = function()
vim.cmd([[colorscheme tokyonight-night]])
end,
},
}

状态栏

这里选择 lualine.nvim,在 ~/.config/nvim/lua/plugins/statusbar.lua 中添加:

1
2
3
4
5
6
7
8
9
10
11
12
return {
{
"nvim-lualine/lualine.nvim",
lazy = false,
dependencies = { "nvim-tree/nvim-web-devicons" },
config = function()
require("lualine").setup({
options = { theme = "tokyonight" },
})
end,
},
}

文件目录树

这里选择 nvim-tree.lua,在 ~/.config/nvim/lua/plugins/tree.lua 中添加:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
return {
{
"nvim-tree/nvim-tree.lua",
dependencies = {
"nvim-tree/nvim-web-devicons",
},
config = function()

vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

require("nvim-tree").setup({
sort = {
sorter = "case_sensitive",
},
view = {
width = 30,
},
renderer = {
group_empty = true,
},
filters = {
dotfiles = true,
},
})

vim.keymap.set("n", "<leader>t", ":NvimTreeToggle<CR>", { silent = true, desc = "Toggle NvimTree" })
end,
},
}

语法高亮与检查配置

语法树高亮与自动括号配对

Neovim 默认不带括号与引号自动补全功能,因此我们在配置 nvim-treesitter(将代码解析为抽象语法树 AST 以提供精准语法高亮与缩进)的同时,一并引入 nvim-autopairs 来实现 ()、[]、{}、""、'' 的自动配对补全。

编辑 ~/.config/nvim/lua/plugins/syntax.lua:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
return {
{
"nvim-treesitter/nvim-treesitter",
lazy = false,
build = ":TSUpdate",
config = function()
local ts = require("nvim-treesitter")
ts.setup()

ts.install({
"c",
"cpp",
"rust",
"go",
"gomod",
"gosum",
"java",
"kotlin",
"python",
"lua",
"vim",
"vimdoc",
"bash",
"cmake",
"make",
"json",
"yaml",
"toml",
"ini",
"xml",
"kdl",
"nginx",
"dockerfile",
"ssh_config",
"git_config",
"gitignore",
"editorconfig",
"desktop",
"udev",
"hyprlang",
"rasi",
"html",
"css",
"javascript",
"typescript",
"tsx",
"sql",
"markdown",
"markdown_inline",
})

vim.api.nvim_create_autocmd("FileType", {
callback = function(args)
if pcall(vim.treesitter.start, args.buf) then
vim.bo[args.buf].indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
end
end,
})
end,
},

{
"windwp/nvim-autopairs",
event = "InsertEnter",
config = function()
require("nvim-autopairs").setup({
check_ts = true,
})
end,
},
}

配置文件语法与格式检查

为了对 JSON、YAML、TOML、XML、Dockerfile、Hyprland 等纯配置文件提供实时的语法与格式错误检查,我们可以使用轻量的 mason.nvim + nvim-lspconfig 组合:

编辑 ~/.config/nvim/lua/plugins/lsp.lua:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
return {
{
"williamboman/mason.nvim",
build = ":MasonUpdate",
config = function()
require("mason").setup({
ui = {
icons = {
package_installed = "✓",
package_pending = "➜",
package_uninstalled = "✗",
},
},
})
end,
},
{
"neovim/nvim-lspconfig",
event = { "BufReadPre", "BufNewFile" },
dependencies = {
"williamboman/mason.nvim",
"williamboman/mason-lspconfig.nvim",
},
config = function()

vim.diagnostic.config({
virtual_text = {
prefix = "●",
},
signs = true,
underline = true,
update_in_insert = false,
severity_sort = true,
})

local config_servers = {
"jsonls",
"yamlls",
"taplo",
"lemminx",
"dockerls",
"docker_compose_language_service",
"hyprls",
}

require("mason-lspconfig").setup({
ensure_installed = config_servers,
automatic_enable = config_servers,
})
end,
},
}

验证与常用命令

完成上述配置后,重新启动 nvim,lazy.nvim 会自动安装所有插件,随后 nvim-treesitter 和 mason 也会在后台自动下载对应的语法解析器和配置文件检查服务。

你可以在 Neovim 中使用以下命令检查运行状态:

  • :Lazy:打开 lazy.nvim 插件管理界面,查看插件加载与更新情况(可按 X 清理不再使用的插件)
  • :checkhealth lazy:检查 lazy.nvim 健康状态
  • :Mason:打开 Mason 管理面板,查看已安装的配置文件
  • :checkhealth vim.lsp(或 :LspInfo):查看当前配置文件挂载的检查服务状态
  • :checkhealth nvim-treesitter:查看 Treesitter 环境及各语言语法解析器的安装状态