Neovim 添加新语言的标准操作流程
这篇笔记基于我的 Neovim 配置进行。
最近学校课程要学 Python,作为 Neovim 用户当然是不会去用老师推荐的 PyCharm 的。要获得比较好的体验就要配置语言服务器(LSP)和 Treesitter,一些语言可能还有其他的辅助工具。为了方便未来配置开发环境,故做此笔记。
LSP
配置 lua/plugins/nvim-lspconfig.lua,找到自己定义的 lsps 变量,在这个数组里添加需要的语言服务器名字。可使用 Telescope 的 help_tags Picker 搜索 lspconfig-all(或者直接 :help lspconfig-all)打开所有 LSP 配置的帮助文档,按下 / 搜索需要的语言,找到准确的语言服务器名称。
例如,Python 的语言服务器可以用 pylsp
local lsps = {
"gopls",
"marksman",
"ts_ls",
"vue_ls",
"cssls",
"html",
"unocss",
"clojure_lsp",
"pylsp"
}
运行 :checkhealth vim.lsp 检查语言服务器状态,比如,配置 pylsp 之后应该能看到这一条。
- pylsp:
- before_init: <function @/Users/eltrac/.local/share/nvim/lazy/mason-lspconfig.nvim/lua/mason-lspconfig/lsp/pylsp.lua:5>
- cmd: { "pylsp" }
- filetypes: python
- root_markers: { "pyproject.toml", "setup.py", "setup.cfg", "requirements.txt", "Pipfile", ".git" }
Treesitter
配置 lua/plugins/nvim-treesitter.lua,在 install({}) 方法中添加语言名称即可安装对应语言的 Treesitter。
require("nvim-treesitter").install({
'javascript',
'html',
'css',
'go',
'clojure',
'gitignore',
'gotmpl',
'markdown',
'lua',
'python'
})
成功后应该能在 :checkhealth 中找到:
Installed languages H L F I J ~
- clojure ✓ ✓ ✓ . ✓
- css ✓ . ✓ ✓ ✓
- ecma
- gitignore ✓ . . . ✓
- go ✓ ✓ ✓ ✓ ✓
- gotmpl ✓ ✓ ✓ . ✓
- html ✓ ✓ ✓ ✓ ✓
- html_tags
- javascript ✓ ✓ ✓ ✓ ✓
- jsx
- lua ✓ ✓ ✓ ✓ ✓
- markdown ✓ . ✓ ✓ ✓
- markdown_inline ✓ . . . ✓
- python ✓ ✓ ✓ ✓ ✓
Legend: [H]ighlights, [L]ocals, [F]olds, [I]ndents, In[J]ections ~
Lisp 方言的额外步骤
为了让 nvim-paredit 正常工作,需要手动启动 Lisp 方言的 Treesitter,因为之前的步骤只是安装了 Treesitter,而 nvim-paredit 不会自己启动 Treesitter。
在 init.lua:
vim.api.nvim_create_autocmd('FileType', {
pattern = { 'clojure' }, // 加上新增的 Lisp 方言
callback = function() vim.treesitter.start() end,
})
应该有更优雅的解决方案,但目前这样也能跑。
其他检查
可以在 :checkhealth 里查找 ERROR 和 WARNING,看看有没有其他问题。
一些语言也有专门给 Neovim 的 Provider,这些 Provider 让 Neovim 拥有对应语言专门的一些特性。
Python 3 provider (optional) ~
- `g:python3_host_prog` is not set. Searching for pynvim-python in the environment.
- Executable: /Users/eltrac/.local/share/../bin/pynvim-python
- Python version: 3.13.1
- pynvim version: 0.6.0
- ✅ OK Latest pynvim is installed.
比如这里的 pynvim 就是一个 Provider,一般来说是单独的二进制文件,和 LSP 一样在独立的进程里运行,并与 Neovim 通信。
在编辑器里执行代码
最简单的方法是在 Neovim 里打开终端,用 :term。比如这是最简单的在 Neovim 里执行当前 Python 文件的命令。
:term python %
个人比较喜欢的方法是用 Conjure 插件,可以在编辑器里直接 Eval1 代码。
有了 LSP 的语言分析能力和 Treesitter 的语法解析能力,配合本身就已经安装了的、会调用 LSP 和 Treesitter 的插件,比如 nvim-cmp(代码补全提示)和 nvim-paredit,又有了在编辑器里直接测试和执行代码的方法(其实就算没有配置编辑器,直接在命令行里执行代码也很好用了),基本上就能应对大部分的编码场景了。
你完全不需要臃肿的 IDE
Evaluate,即计算值的意思。 ↩︎
2026-03-05