Neovim 插件
启动界面
- alpha-nvim
alpha-nvim 插件做了三件事情:
- 顶部添加 ASCII 艺术字体制作的 LOGO
- 中部添加一组快捷键
- 底部添加名言警句,每次启动随机出现
其中,快捷键的实现,背后起作用的是命令行命令,有的是内置的 Vim 命令,有的是其他插件引入的命令。例如:
e
→ Opens a new fileSPC ee
→ Toggles the file explorer (NvimTree
)SPC ff
→ Finds a file usingFzfLua
SPC fg
→ Searches for a word usingFzfLua
SPC wr
→ Restores a session usingAutoSession
q
→ Quits Neovim
除了 alpha-nvim 之外,其他类似的设置启动界面的插件还有:
- dashboard-nvim
- startup.nvim
- vim-startify
- mini.starter
界面美化
- tokyonight.nvim
- lualine.nvim
- indent-blankline.nvim
文件管理
Vim/Neovim 有一个内置的文件管理器 netrw,可以通过命令行运行 :Explorer
调出,但是相关的快捷命令很难记忆,较少使用。
进入 Neovim 编辑界面,左边使用 nvim-tree 生成的侧边栏,以树形目录进行导航;顶部使用一个 bufferline.nvim 生成的标签页面,进行 tab 窗口管理。
- nvim-tree
- bufferline.nvim
Vim/Neovim 中的 tag 标签页面,跟 VS Code 中的 tag 标签页面,外在表现形式有一点点类似,但本质上又不完全相同。
- VS Code 中的标签页面就是 一个 打开的文件
- Vim/Neovim 中的标签页面是 多个 buffer 的容器
用于文件管理的另外一个流行的插件是:
- oil.nvim
oil.nvim 设计理念基于“将当前目录下的文件列表视作可以一份可以编辑、修改的 buffer”,并且目录不是以树形结构嵌套呈现,每次操作仅显示当前目录下、同一层级的文件列表。
代码导航
- nvim-treesitter: Treesitter configurations and abstractions layer for Neovim
- nvim-treesitter-textobjects
Treesitter 可以根据解析器为每一份文件生成一棵语法树。即是说,每份文件都可以被 Treesitter 结构化,然后 Neovim 可以根据解析出来的文本结构做一些有用的事情。
nvim-treesitter
- 高亮显示
- 步进选择
- 缩进
- 折叠
nvim-treesitter-textobjects
扩展 Neovim 文本对象的概念,实现:
- 选择文本对象
- 对调光标所在节点
- 跳转至 下一个/上一个 文件对象
文件搜索
- fzf-lua
用于文件搜索的另外一个流行的插件是:
- telescope.nvim
LSP
- nvim-lspconfig
- mason.nvim
- mason-lspconfig.nvim
- mason-tool-installer.nvim
这一系列关于 LSP 的插件是根据 Mr Jakob 的 nvim-from-scratch 系列视频的第 4 集,做了重新的配置。在 Lua 配置文件内,会生成 "Undefined global vim
" 警告信息。
为消除这些令人分神的警告信息,在 plugins 目录内创建 lazydev.lua 文件,添加如下代码:
return {
"folke/lazydev.nvim",
ft = "lua", -- only load on lua files
opts = {
library = {},
enabled = true,
},
}
lazydev.nvim
always injects Neovim’s core API (including the vim
global) into the Lua Language Server’s (LSP) scope by default, even if you don’t specify it in library
. This is because:
- The plugin is specifically built for Neovim Lua development
- It ships with predefined type declarations for
vim
and other Neovim globals (e.g.,describe
,it
for test suites)
如果你在 ~/.config/nvim
只有一套 Neovim 的配置,那么上述配置已经足够了。如果你还有另外一套 Neovim 配置,例如保存在 ~/.config/nvim-from-scratch
,那么上述配置在使用第二套配置时,是无法消除警告信息的。经过再次测试,上述配置对于第二套配置好像已经足够,无须再做下面的额外配置。
Root Cause
Neovim’s Runtime Path Detection
lazydev.nvim
implicitly injects thevim
global by telling the LSP where to find Neovim’s type definitions (e.g.,vim.d.lua
). In the default~/.config/nvim
, the LSP automatically detects Neovim’s runtime path. In~/.config/nvim-from-scratch
, the LSP might fail to locate these files due to:- Missing symlinks to Neovim’s runtime
- Incorrect
LUA_PATH
orruntimepath
settings
LSP Workspace Scope The LSP treats each config folder (
nvim
vs.nvim-from-scratch
) as a separate workspace. If it can’t resolve Neovim’s types, it throwsundefined global
warnings.
Solutions
- Force-Load Neovim's Runtime Types (Recommended)
Expplicitly point the LSP to Neovim's runtime files in your lazydev.lua
:
return {
"folke/lazydev.nvim",
ft = "lua", -- only load on lua files
opts = {
library = {
-- Add Neovim's runtime Lua files (macOS default location)
vim.fn.stdpath("config") .. "/lua",
vim.fn.stdpath("data") .. "/lazy/lazy.nvim/lua/lazy", -- For lazy.nvim
},
enabled = true,
},
}
Why this works:
stdpath("config")
resolves to~/.config/nvim-from-scratch
- The LSP will scan these paths for type definitions (including
vim
)
- Verify
runtimepath
Run this in nvim-from-scratch
to check if Neovim’s runtime is visible:
:lua print(vim.inspect(vim.api.nvim_get_runtime_file("lua", true)))
- If the output doesn’t include paths like
/usr/share/nvim/runtime/lua
(or Homebrew’s Neovim path), the LSP can’t find types - Both nvim and nvim-from-scratch config output includes
/opt/homebrew/Cellar/neovim/0.11.0/share/nvim/runtime/lua
格式化工具
- vim-sleuth
- conform.nvim
- pangu.vim
快捷编辑
- auto-session
- nvim-surround
- nvim-autopairs
- bullets.vim
- Comment.nvim
代码补全
- blink.cmp
- blink.compat
git 版本控制
- lazygit.nvim
- gitsigns.nvim
其他
- dressing.nvim
- snacks.nvim
在当前的配置中,这个插件有什么用途?是还可以删除?