Ubuntu 22.04 Neovim 安装与命令参数详解

Ubuntu 22.04 下 Neovim 0.6.1 在线/离线安装与全部核心命令参数(headless/-c/-es/lua/alternatives)的真实回显详解

Linux开发与编译工具链neovim2026/10/06

Ubuntu 22.04 Neovim 安装与命令参数详解

目标:在 Ubuntu 22.04 LTS x86_64 服务器上,从 0 到 1 完成 Neovim 0.6.1 的在线安装与离线安装,并对 --version、--headless、-c、-es、+checkhealth、:scriptnames、:echo has("...")、init.vim/init.lua 等核心命令与参数逐一实测,给出真实输入与真实回显,同时实测 Ubuntu 特有的 vim/nvim alternatives 冲突与修复。

一、环境信息

项目值
操作系统Ubuntu 22.04 LTS(jammy,内核实测 PRETTY_NAME="Ubuntu 22.04.5 LTS")
架构x86_64
包管理器apt(底层 dpkg)
Neovim 版本0.6.1(软件包版本 0.6.1-3,universe 源)
内嵌 LuaLuaJIT 2.1.0-beta3
登录用户root(普通用户操作时以 sudo 前缀执行)

首先确认系统与 Neovim 现状:

cat /etc/os-release | head -3

回显:

PRETTY_NAME="Ubuntu 22.04.5 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
uname -m

回显:

x86_64
nvim --version | head -1

回显:

NVIM v0.6.1

💡 说明:本文所有命令回显均在 Ubuntu 22.04 x86_64 实测环境逐行采集;文档中的演示文件统一放在 /opt/devchain-demo/neovim-ubuntu-test/ 目录下。

二、工具概述

Neovim(简称 nvim)是 Vim 的一个现代化重构分支(fork),由社区于 2014 年发起,目标是在保持 Vim 编辑体验与键位习惯不变的前提下,用现代工程方式重写核心代码,解决 Vim 的插件生态、异步 IO 与扩展性短板。Ubuntu 22.04 仓库内置 Neovim 0.6.1。

Neovim 的核心功能包括:

  • 内嵌 Lua 运行时:LuaJIT 直接编译进 nvim 二进制,用户配置与插件既可以写 Vimscript,也可以写 Lua(init.vim 之外还支持 init.lua、:lua 命令、lua 函数)。
  • 异步 API(msgpack-RPC):通过 msgpack 结构化协议与外部程序通信,远程插件(remote plugin)作为独立进程与 nvim 安全异步交互。
  • 内置终端模拟器:可在 nvim 内直接打开一个完整终端(:terminal),无需退出编辑器即可运行编译、测试等命令。
  • 无界面模式(headless):--headless 参数让 nvim 不启动任何用户界面运行,非常适合在脚本、CI 流水线中做批量文本处理、自动化测试与配置验证。
  • 现代插件机制:原生支持 Lua 插件、tree-sitter 语法解析(0.6 起引入)、内置 LSP 客户端雏形等。

在 Linux 开发与编译工具链中,Neovim 与 Vim 共同承担"编辑器"角色:使用 nvim 编写 C/C++ 源码 → 交给 gcc/g++ 编译 → make 构建 → gdb 调试,全流程无需离开编辑器。与 Vim 相比,Neovim 的优势在于 Lua 配置生态、异步处理与更强的可扩展性;而 Vim 胜在"系统自带、随处可用"。

典型使用场景:

  • 作为日常代码编辑器,配合 Lua 配置与插件实现补全、跳转、格式化。
  • 使用 --headless/-es 模式在服务器上做脚本化文本处理(批量替换、格式化)。
  • 通过 nvim +checkhealth 快速诊断环境问题(如剪贴板、终端能力、Locale)。

三、在线安装方案

Ubuntu 22.04 官方软件源(archive.ubuntu.com 的 jammy 系列)已内置 Neovim 软件包,直接使用 apt 安装即可。

3.1 更新软件源索引

sudo apt update

回显(末尾片段):

Hit:3 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:4 http://security.ubuntu.com/ubuntu jammy-security InRelease
Reading package lists...
Building dependency tree...
Reading state information...
8 packages can be upgraded. Run 'apt list --upgradable' to see them.

3.2 安装 Neovim

sudo apt install -y neovim

回显:


WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Reading package lists...
Building dependency tree...
Reading state information...
neovim is already the newest version (0.6.1-3).
0 upgraded, 0 newly installed, 0 to remove and 8 not upgraded.

💡 说明:本机已安装过 Neovim,因此 apt 提示 "neovim is already the newest version (0.6.1-3)"。若首次安装,apt 会显示 "Need to get ... kB of archives"、解包与配置进度,最终出现 Setting up neovim (0.6.1-3) ... 即安装成功。

3.3 查看软件源中的候选版本

apt-cache policy neovim

回显:

neovim:
  Installed: 0.6.1-3
  Candidate: 0.6.1-3
  Version table:
 *** 0.6.1-3 500
        500 http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages
        100 /var/lib/dpkg/status

⚠️ 注意:Installed 表示当前已装版本,Candidate 表示 apt 认为可升级到的候选版本,此处两者一致均为 0.6.1-3,说明已是最新。Neovim 在 Ubuntu 仓库中位于 universe 组件,若服务器裁剪过源配置,需确认 universe 组件已启用。

3.4 验证安装

nvim --version | head -1

回显:

NVIM v0.6.1
which nvim

回显:

/usr/bin/nvim

四、离线安装方案

离线安装的核心思路:在可联网的机器上用 apt 只下载不安装(--download-only),得到 .deb 安装包;将安装包拷贝到离线服务器后,用 dpkg -i 直接安装。

4.1 仅下载 neovim 安装包

sudo apt install --reinstall --download-only -y neovim

回显:


Reading package lists...
Building dependency tree...
Reading state information...
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 8 not upgraded.
Need to get 1814 kB of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy/universe amd64 neovim amd64 0.6.1-3 [1814 kB]
Fetched 1814 kB in 3s (619 kB/s)
Download complete and in download only mode

💡 说明:--download-only 让 apt 只把包下载到 /var/cache/apt/archives/ 而不安装;--reinstall 保证即使包已安装也会重新下载。

4.2 确认下载的 deb 包

ls -lh /var/cache/apt/archives/*.deb

回显:

-rw-r--r-- 1 root root 1.8M Jan  3  2022 /var/cache/apt/archives/neovim_0.6.1-3_amd64.deb

4.3 拷贝到离线分发目录

sudo mkdir -p /opt/devchain-demo/neovim-ubuntu-test/offline
sudo cp /var/cache/apt/archives/neovim_0.6.1-3_amd64.deb /opt/devchain-demo/neovim-ubuntu-test/offline/
ls -lh /opt/devchain-demo/neovim-ubuntu-test/offline

回显:

total 1.8M
-rw-r--r-- 1 root root 1.8M Oct  6 06:34 neovim_0.6.1-3_amd64.deb

4.4 查看 deb 包元信息(依赖与描述)

dpkg-deb -I /opt/devchain-demo/neovim-ubuntu-test/offline/neovim_0.6.1-3_amd64.deb

回显:

 new Debian package, version 2.0.
 size 1813554 bytes: control archive=1380 bytes.
    1060 bytes,    23 lines      control
     479 bytes,     8 lines      md5sums
     750 bytes,    18 lines   *  postinst             #!/bin/sh
     663 bytes,    28 lines   *  prerm                #!/bin/sh
 Package: neovim
 Version: 0.6.1-3
 Architecture: amd64
 Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
 Original-Maintainer: Debian Vim Maintainers <team+vim@tracker.debian.org>
 Installed-Size: 4450
 Depends: neovim-runtime (= 0.6.1-3), libc6 (>= 2.34), libluajit-5.1-2 (>= 2.0.4+dfsg), libmsgpackc2 (>= 2.1.0), libtermkey1 (>= 0.19), libtree-sitter0 (>= 0.20.0), libunibilium4 (>= 2.0), libuv1 (>= 1.30.1), libvterm0 (>= 0.1), lua-luv (>= 1.36.0-0)
 Recommends: python3-neovim, xclip | xsel, xxd
 Suggests: ctags, vim-scripts
 Provides: editor
 Section: editors
 Priority: optional
 Homepage: https://neovim.io/
 Description: heavily refactored vim fork
  Neovim is a fork of Vim focused on modern code and features, rather than
  running in legacy environments.
  .
  msgpack API enables structured communication to/from any programming language.
  Remote plugins run as co-processes that communicate with Neovim safely and
  asynchronously.
  .
  GUIs (or TUIs) can easily embed Neovim or communicate via TCP sockets using
  the discoverable msgpack API.

⚠️ 注意:由 Depends 可见,neovim 主包依赖 neovim-runtime(同版本)以及 libc6、libluajit-5.1-2、libmsgpackc2、libtermkey1、libtree-sitter0、libunibilium4、libuv1、libvterm0、lua-luv 等库。离线环境若要完整安装,需按同样方式把 neovim-runtime 与这些依赖库的 deb 一并下载。实测本机这些依赖库均已安装(neovim 可正常运行),仅需额外携带 neovim-runtime_0.6.1-3_amd64.deb(可用 apt-cache depends neovim 查看完整依赖列表)。

4.5 查看安装包在系统中的文件布局

dpkg -L neovim | head -20

回显:

/.
/usr
/usr/bin
/usr/bin/nvim
/usr/libexec
/usr/libexec/neovim
/usr/libexec/neovim/ex
/usr/libexec/neovim/rview
/usr/libexec/neovim/rvim
/usr/libexec/neovim/view
/usr/libexec/neovim/vimdiff
/usr/share
/usr/share/doc
/usr/share/doc/neovim
/usr/share/doc/neovim/changelog.Debian.gz
/usr/share/doc/neovim/copyright

💡 说明:主包内只有 /usr/bin/nvim 一个可执行文件,以及 /usr/libexec/neovim/ 下的 ex、rview、rvim、view、vimdiff 兼容入口;运行时资源(语法、插件、帮助)都在 neovim-runtime 包中(/usr/share/nvim/)。

4.6 在离线服务器上安装

sudo dpkg -i /opt/devchain-demo/neovim-ubuntu-test/offline/neovim_0.6.1-3_amd64.deb

💡 说明:若缺依赖,dpkg -i 会报依赖未满足错误,此时应把随包携带的 neovim-runtime 与依赖库 deb 放在同一目录,先执行 sudo dpkg -i 全部 deb,或用 sudo apt install -y /opt/offline/*.deb 让 apt 自动解析本地 deb 依赖。安装完成后同样用 nvim --version 验证。

五、命令与参数详解

💡 说明:本节的每个参数都给出"一行说明 + 真实命令 + 真实回显"。演示用文件 /opt/devchain-demo/neovim-ubuntu-test/demo.txt 初始内容如下(后续命令会不断改写它):

cat /opt/devchain-demo/neovim-ubuntu-test/demo.txt

回显:

hello world
this is line two
old text here
last line

5.1 版本信息:nvim --version

--version(或 -v)打印完整版本信息:版本号、构建类型、内嵌 LuaJIT 版本、编译方、编译特性(Features)、系统级配置文件位置等。

nvim --version

回显:

NVIM v0.6.1
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by team+vim@tracker.debian.org

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info

⚠️ 注意:Ubuntu 22.04 打包的 nvim 0.6.1 编译特性仅 +acl +iconv +tui,没有 +lua 特性标志(尽管二进制内嵌了 LuaJIT,:lua 仍可用,见 5.3 节与 FAQ)。system vimrc file 指向 $VIM/sysinit.vim,$VIM 的 fall-back 路径为 /usr/share/nvim。

5.2 帮助信息:nvim -h

-h(或 --help)打印命令行用法与全部启动选项。

nvim -h

回显(关键片段):

Usage:
  nvim [options] [file ...]      Edit file(s)
  nvim [options] -t <tag>        Edit file where tag is defined
  nvim [options] -q [errorfile]  Edit file with first error

Options:
  --                    Only file names after this
  +                     Start at end of file
  --cmd <cmd>           Execute <cmd> before any config
  +<cmd>, -c <cmd>      Execute <cmd> after config and first file

  -b                    Binary mode
  -d                    Diff mode
  -e, -E                Ex mode
  -es, -Es              Silent (batch) mode
  -h, --help            Print this help message
  -i <shada>            Use this shada file
  -m                    Modifications (writing files) not allowed
  -M                    Modifications in text not allowed
  -n                    No swap file, use memory only
  -o[N]                 Open N windows (default: one per file)
  -O[N]                 Open N vertical windows (default: one per file)
  -p[N]                 Open N tab pages (default: one per file)
  -r, -L                List swap files
  -r <file>             Recover edit state for this file
  -R                    Read-only mode
  -S <session>          Source <session> after loading the first file
  -s <scriptin>         Read Normal mode commands from <scriptin>
  -u <config>           Use this config file
  -v, --version         Print version information
  -V[N][file]           Verbose [level][file]

  --api-info            Write msgpack-encoded API metadata to stdout
  --embed               Use stdin/stdout as a msgpack-rpc channel
  --headless            Don't start a user interface
  --listen <address>    Serve RPC API from this address
  --noplugin            Don't load plugins
  --startuptime <file>  Write startup timing messages to <file>

See ":help startup-options" for all options.

💡 说明:重点掌握 -c <cmd>(进入第一个文件后执行 Ex 命令,是脚本化核心)、-es(静默批处理模式,输出最干净)、--headless(不启动 UI,适合服务端/脚本)、-u <config>(指定配置文件)、--startuptime(分析启动性能)。

5.3 无界面模式:--headless 与 -c 命令

--headless 让 nvim 不启动任何用户界面(无 TUI、无 GUI),配合 -c '命令' 逐条执行 Ex 命令,最后用 -c 'q'(或 -c 'wq')退出。这是服务器端脚本化操作 Neovim 的标准方式。

5.3.1 输出文本:-c 'echo "..."'

nvim --headless -c 'echo "hello"' -c 'q'

回显:

hello

-c 后面的 Ex 命令在配置文件加载之后、进入第一个文件之后执行;多条 -c 按从左到右顺序执行。

5.3.2 执行 Lua:-c 'lua ...'

0.6.1 内嵌 LuaJIT,-c 'lua <代码>' 直接执行一行 Lua 代码。

nvim --headless -c 'lua print(1+1)' -c 'q'

回显:

2
nvim --headless -c "lua vim.api.nvim_echo({{'hello from lua'}}, true, {})" -c 'q'

回显:

hello from lua

💡 说明:vim.api.nvim_echo 是 0.6 时代的原生 API 函数,参数为 {{{text}}, attr, {}:第一层 {{'hello from lua'}} 是"消息数组套数组",true 表示历史不进消息记录,空 {} 表示无高亮属性。它把消息直接打印到 stdout(headless 下可见)。

nvim --headless -c 'lua local t={1,2,3}; print(t[1])' -c 'q'

回显:

1

⚠️ 注意:-c 'lua ...' 若 Lua 代码里包含引号嵌套,务必保证整体参数被 shell 正确引用。例如 nvim_echo 示例中 Lua 字符串用了单引号,整条 -c 参数再用双引号包裹;若引号配对错误,nvim 会报 E5107: Error loading lua(详见 FAQ)。

5.3.3 普通模式按键序列:-c 'norm ...'

norm(normal)命令在 Ex 模式里执行一串普通模式按键,等价于在交互界面里按下对应按键。这是"交互按键序列"脚本化验证的手段。

插入文本 i(光标前插入)/ a(光标后追加)/ o(当前行下方新建行)

nvim --headless -c '1' -c 'norm iINSERT:' -c 'wq' demo.txt

回显(wq 写入消息):

"demo.txt" "demo.txt" 4L, 60C written
cat demo.txt

回显:

INSERT:hello world
this is line two
old text here
last line

💡 说明:'1' 先把光标定位到第 1 行,norm iINSERT: 等价于交互模式按 i 进入插入模式输入 "INSERT:",Esc 返回普通模式(norm 的字符串模拟了完整按键流)。

printf 'hello world\nthis is line two\nold text here\nlast line\n' > demo.txt
nvim --headless -c 'norm A [appended]' -c 'wq' demo.txt

回显:

"demo.txt" "demo.txt" 4L, 64C written
cat demo.txt

回显:

hello world [appended]
this is line two
old text here
last line

💡 说明:A 在行尾追加(等价于 $a);上一条 printf 用于把 demo.txt 恢复到初始内容,避免前一条命令的改动干扰本条演示。

printf 'hello world\nthis is line two\nold text here\nlast line\n' > demo.txt
nvim --headless -c '1' -c 'norm oNEWLINE' -c 'wq' demo.txt

回显:

"demo.txt" "demo.txt" 5L, 61C written
cat demo.txt

回显:

hello world
NEWLINE
this is line two
old text here
last line

💡 说明:o 在光标所在行下方新建一行并进入插入模式,输入 "NEWLINE";O(大写)则在光标所在行上方新建一行。

删除行 dd / 复制粘贴 yy p

printf 'hello world\nthis is line two\nold text here\nlast line\n' > demo.txt
nvim --headless -c 'norm dd' -c 'wq' demo.txt

回显:

"demo.txt" "demo.txt" 3L, 41C written
cat demo.txt

回显:

this is line two
old text here
last line

💡 说明:dd 删除光标所在整行,删除的内容同时进入无名寄存器 "" 与编号寄存器 "1;文件从 4 行 49 字符变为 3 行 41 字符。

printf 'hello world\nthis is line two\nold text here\nlast line\n' > demo.txt
nvim --headless -c '1' -c 'norm yy' -c 'norm p' -c 'wq' demo.txt

回显:

"demo.txt" "demo.txt" 5L, 65C written
cat demo.txt

回显:

hello world
hello world
this is line two
old text here
last line

💡 说明:yy 复制(yank)整行,p 在光标所在行下方粘贴,因此第 1 行 "hello world" 被复制了一份(文件从 4 行变 5 行);P(大写)则在上方粘贴。

光标跳转 gg(跳到文件首行)/ G(跳到文件末行)

printf 'hello world\nthis is line two\nold text here\nlast line\n' > demo.txt
nvim --headless -c 'norm G' -c 'norm gg' -c 'echo line(".")' -c 'q!' demo.txt

回显:

1
nvim --headless -c 'norm G' -c 'echo line(".")' -c 'q!' demo.txt

回显:

4

💡 说明:echo line(".") 打印光标所在行号。第一条命令先 G 跳到末行再 gg 跳回首行,结果为 1;第二条只 G,结果为 4(共 4 行)。q! 不保存直接退出。

撤销 u 与重做 Ctrl-R

printf 'hello world\nthis is line two\nold text here\nlast line\n' > demo.txt
nvim --headless -c 'norm dd' -c 'norm u' -c 'wq' demo.txt

回显:

1 more line; before #1  0 seconds ago
"demo.txt" "demo.txt" 4L, 53C written
cat demo.txt

回显:

hello world
this is line two
old text here
last line

💡 说明:norm dd 删除第 1 行后,norm u 撤销删除,回显 1 more line; before #1 0 seconds ago,文件恢复为 4 行。

重做(redo)对应交互模式按键 Ctrl-R,脚本中可用真实控制字符(十六进制 0x12)触发:

printf 'hello world\nthis is line two\nold text here\nlast line\n' > demo.txt
nvim --headless -c 'norm dd' -c 'norm u' -c $'norm \x12' -c 'wq' demo.txt

回显:

1 more line; before #1  0 seconds ago
1 line less; after #1  0 seconds ago
"demo.txt" "demo.txt" 3L, 41C written
cat demo.txt

回显:

this is line two
old text here
last line

💡 说明:$'norm \x12' 中的 \x12 是 Ctrl-R 的 ASCII 码,等价于交互模式按 Ctrl-R;回显 1 line less; after #1 表明重做成功,第 1 行再次被删除(文件回到 3 行)。

5.4 静默批处理模式:-es

-es(Silent Ex mode,-Es 同理)是输出最干净的批处理模式:只执行命令,不进入交互界面,错误信息也会被抑制。典型用途:管道脚本、CI 里的批量文本替换。

5.4.1 全局替换 :%s

nvim -es -c '1,$s/old/new/g' -c 'wq' demo.txt

💡 说明:1,$ 表示从第 1 行到末行,s/old/new/g 将行内所有 "old" 替换为 "new"(g 标志表示一行内全部替换),wq 保存并退出。此命令无 stdout 输出,用 cat 验证结果。

cat demo.txt

回显:

hello world
this is line two
new text here
last line

⚠️ 注意:替换前的 demo.txt 内容为 hello world / this is line two / old text here / last line(上一节演示后的状态),因此 "old text here" 变为 "new text here"。

5.4.2 普通模式按键批处理

-es 同样支持 norm 按键流,适合对多个文件执行同一组操作(例如删除首行、去空行等):

nvim -es -c 'norm dd' -c 'wq' demo.txt

回显(写入消息):

"demo.txt" "demo.txt" 3L, 41C written

💡 说明:-es 与 -es 的区别:-e 进入 Ex 模式(有回显与提示符),-es 是 silent 版本(无回显、错误信息仅在 vim -V 等场景输出),批处理首选 -es。

5.5 搜索:/pattern

交互模式下 /pattern 向下搜索(?pattern 向上搜索)。脚本化可用 search("pattern") 函数并配合 getline(".") 查看命中行:

printf 'hello world\nthis is line two\nold text here\nlast line\n' > demo.txt
nvim --headless -c '/world' -c 'echo search("world")' -c 'echo getline(".")' -c 'q!' demo.txt

回显:

1
hello world

💡 说明:/world 设置搜索模式并移动光标到首个匹配;search("world") 返回匹配行号 1;getline(".") 返回当前行内容 "hello world"。匹配到的模式会写入搜索历史寄存器 "/(见 5.8 节 :reg 回显中的 c "/ old)。

5.6 特性检测::echo has("...") 与 v:version

has("特性") 返回 1(支持)或 0(不支持),用于脚本中做特性分支;v:version 是 Vimscript 兼容层报告的版本号。

nvim --headless -c 'echo has("lua")' -c 'q'

回显:

0

⚠️ 注意:实测 Ubuntu 22.04 的 neovim 0.6.1-3 中 has("lua") 返回 0,因为该打包版本编译特性表只有 +acl +iconv +tui(无 +lua)。但内嵌 LuaJIT 本身可用(5.3.2 节 :lua 命令实测正常执行),判断"能否用 Lua"应直接用 :lua 或检查 luaeval() 是否报错,详见 FAQ。

nvim --headless -c 'echo has("nvim-0.6")' -c 'q'

回显:

1

💡 说明:has("nvim-0.6") 用于判断 Neovim 主版本 ≥ 0.6,脚本中常配合 has("nvim-0.5")/has("nvim-0.7") 做版本分档。

nvim --headless -c 'echo v:version' -c 'q'

回显:

800

💡 说明:v:version 是 Neovim 为兼容 Vimscript 保留的版本号字段,恒定报告 800(对应 Vim 8.0 级别的特性基线),不代表 Neovim 自身版本 0.6.1;判断 nvim 版本请用 has("nvim-0.6") 或 nvim --version。

5.7 加载顺序::scriptnames

:scriptnames 列出本次启动中所有已加载脚本(配置文件、运行时脚本、插件),编号即加载顺序。scriptnames 可作为 headless 命令在服务器上直接查看:

nvim --headless -c 'scriptnames' -c 'q'

回显:

  1: ~/.config/nvim/init.vim
  2: /usr/share/nvim/runtime/filetype.vim
  3: /usr/share/nvim/runtime/ftplugin.vim
  4: /usr/share/nvim/runtime/indent.vim
  5: /usr/share/nvim/runtime/syntax/syntax.vim
  6: /usr/share/nvim/runtime/syntax/synload.vim
  7: /usr/share/nvim/runtime/plugin/gzip.vim
  8: /usr/share/nvim/runtime/plugin/health.vim
  9: /usr/share/nvim/runtime/plugin/man.vim
 10: /usr/share/nvim/runtime/plugin/matchit.vim
 11: /usr/share/nvim/runtime/pack/dist/opt/matchit/plugin/matchit.vim
 12: /usr/share/nvim/runtime/plugin/matchparen.vim
 13: /usr/share/nvim/runtime/plugin/netrwPlugin.vim
 14: /usr/share/nvim/runtime/plugin/rplugin.vim
 15: /usr/share/nvim/runtime/plugin/shada.vim
 16: /usr/share/nvim/runtime/plugin/spellfile.vim
 17: /usr/share/nvim/runtime/plugin/tarPlugin.vim
 18: /usr/share/nvim/runtime/plugin/tohtml.vim
 19: /usr/share/nvim/runtime/plugin/tutor.vim
 20: /usr/share/nvim/runtime/plugin/zipPlugin.vim

💡 说明:用户配置文件 ~/.config/nvim/init.vim 编号为 1,最先加载;随后是运行时基础脚本(filetype/indent/syntax),最后是自带插件(/usr/share/nvim/runtime/plugin/)。若配置被改为 init.lua,第 1 行会显示 ~/.config/nvim/init.lua(见 6.4 节实测)。

5.8 健康检查:+checkhealth

+checkhealth(等价 -c 'checkhealth')运行健康检查,诊断配置、性能、远程插件、终端、剪贴板等环境项。在服务器终端执行:

nvim +checkhealth

回显(无插件环境的实测结果):

nvim: health#nvim#check
========================================================================
## Configuration
  - ERROR: Locale does not support UTF-8. Unicode characters may not
    display correctly.
    $LANG= $LC_ALL= $LC_CTYPE=
    - ADVICE:
      - If using tmux, try the -u option.
      - Ensure that your terminal/shell/tmux/etc inherits the environment,
        or set $LANG explicitly.
      - Configure your system locale.

## Performance
  - OK: Build type: Release

## Remote Plugins
  - OK: Up to date

## terminal
  - ERROR: command failed: infocmp -L

⚠️ 注意:实测本机未设置 LANG/LC_ALL/LC_CTYPE,因此 Configuration 报 Locale ERROR;同时缺少终端 terminfo 工具(infocmp)导致 terminal 项报错。这两项不影响编辑与脚本化使用,但会影响 UTF-8 显示与终端能力探测,建议按 ADVICE 设置 LANG 并安装 ncurses-bin(提供 infocmp)。若安装了插件,+checkhealth 会额外列出各插件的健康项;:checkhealth <插件名> 可单独检查某插件。

5.9 寄存器::reg

:reg(:registers 的简写)列出当前所有寄存器内容,包括无名寄存器 ""、编号寄存器 "0-"9、删除寄存器 "1-"9、剪贴板寄存器、文件名寄存器 "%、搜索寄存器 "/ 等。先复制一行再查看:

nvim --headless -c '1' -c 'norm yy' -c 'reg' -c 'q!' demo.txt

回显:


Type Name Content
  l  ""   hello world^J
  l  "0   hello world^J
  l  "1   hello world^J
  l  "2   hello world^J
  l  "3   hello world^J
clipboard: No provider. Try ":checkhealth" or ":h clipboard".
clipboard: No provider. Try ":checkhealth" or ":h clipboard".
  c  "%   demo.txt
  c  "/   old

💡 说明:yy 复制后,""(无名)与 "0(最近复制)都保存了 "hello world";"1-"3 保存最近三次删除/复制历史;"% 是当前文件名 demo.txt;"/ 是最近搜索模式(此前 5.5 节搜索过 "old")。末尾两条 clipboard 提示说明服务器未配置剪贴板提供者(如 xclip),headless 模式下属正常现象。

5.10 分屏::vsplit

:vsplit(:vsp)垂直分屏,:split 水平分屏。headless 下用 winnr() 验证窗口创建:

nvim --headless -c 'vsplit' -c 'echo winnr()' -c 'q!' demo.txt

回显:

1

💡 说明:vsplit 在交互界面中会把窗口一分为二(左侧原文件、右侧新窗口);winnr() 返回当前窗口编号。headless 下窗口管理同样生效,便于脚本验证多窗口逻辑。窗口间跳转按键:Ctrl-w w(循环切换)、Ctrl-w h/j/k/l(按方向切换)、:q(关闭当前窗口)。

5.11 设置选项::set number 与选项查询

:set <选项> 设置选项;:echo &<选项> 读取选项当前值。number 显示行号,tabstop 设置 Tab 对应空格数:

nvim --headless -c 'set number' -c 'echo &number' -c 'q'

回显:

1
nvim --headless -c 'set tabstop=4' -c 'echo &tabstop' -c 'q'

回显:

4

💡 说明:&number 值为 1 表示行号已开启,0 表示关闭;&tabstop 返回当前 Tab 宽度。无配置时默认 &number=0、&tabstop=8(见 6.5 节实测)。常用开关选项还有 set nowrap/set wrap、set ignorecase、set relativenumber 等,均可按此方式脚本化验证。

5.12 与 Vim 的关系:Ubuntu alternatives 冲突与修复(实测)

Ubuntu 用 update-alternatives(Debian alternatives 机制)管理 editor、vim、view、vimdiff 等"虚拟编辑器"链接。实测 neovim 包安装后会把自己注册进 vim 组与 editor 组,可能与 Vim 产生接管冲突。本节完整实测冲突现象与修复闭环。

5.12.1 查看 editor 组当前指向

update-alternatives --display editor

回显(关键部分):

editor - auto mode
  link best version is /usr/bin/nvim
  link currently points to /usr/bin/nvim
  link editor is /usr/bin/editor
  slave editor.1.gz is /usr/share/man/man1/editor.1.gz
  ...
/usr/bin/nvim - priority 30
  slave editor.1.gz: /usr/share/man/man1/nvim.1.gz
/usr/bin/vim.basic - priority 30
  slave editor.1.gz: /usr/share/man/man1/vim.1.gz
  ...

💡 说明:editor 组处于 auto 模式,候选为 /usr/bin/nvim 与 /usr/bin/vim.basic(priority 均为 30),当前 best/currently 都指向 nvim,即系统默认编辑器 /usr/bin/editor 实际运行的是 Neovim。实测 editor --version | head -1 输出 NVIM v0.6.1 佐证这一点。

5.12.2 查看 vim 组候选

update-alternatives --list vim

回显:

/usr/bin/nvim
/usr/bin/vim.basic

⚠️ 注意:vim 组中同时注册了 nvim 与 vim.basic,说明 neovim 包通过 postinst 脚本把自己的可执行文件挂进了 vim 组。虽然实测基线状态下 vim 组 manual 模式指向 /usr/bin/vim.basic(vim --version 输出 VIM 8.2),但一旦有人执行 update-alternatives --config vim 误选 nvim,或系统策略变化,vim 命令就会被"接管"成 Neovim。

5.12.3 查看可执行文件链接关系

ls -l /usr/bin/vim* /usr/bin/nvim*

回显:

-rwxr-xr-x 1 root root 4506280 Jan  3  2022 /usr/bin/nvim
lrwxrwxrwx 1 root root      21 Oct  6 04:54 /usr/bin/vim -> /etc/alternatives/vim
-rwxr-xr-x 1 root root 3787952 Aug 24 22:13 /usr/bin/vim.basic
lrwxrwxrwx 1 root root      25 Oct  6 04:54 /usr/bin/vimdiff -> /etc/alternatives/vimdiff
-rwxr-xr-x 1 root root    2154 Aug 24 22:13 /usr/bin/vimtutor

💡 说明:/usr/bin/vim 是指向 /etc/alternatives/vim 的符号链接(由 alternatives 管理);真实二进制是 /usr/bin/vim.basic(Vim 8.2)与 /usr/bin/nvim(Neovim 0.6.1)。基线状态下 vim 命令运行的仍是 Vim:

vim --version | head -1

回显:

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Aug 24 2026 22:13:24)

5.12.4 接管演示:把 vim 切到 nvim

update-alternatives --set vim /usr/bin/nvim

回显:

update-alternatives: using /usr/bin/nvim to provide /usr/bin/vim (vim) in manual mode

验证 vim 命令已被接管:

vim --version | head -1

回显:

NVIM v0.6.1
ls -l /etc/alternatives/vim

回显:

lrwxrwxrwx 1 root root 13 Oct  6 06:41 /etc/alternatives/vim -> /usr/bin/nvim

⚠️ 注意:此时 vim、vi(若 vi 也指向该组)命令全部变成运行 Neovim。若用户并不希望这样,需要"修复"。

5.12.5 修复:切回 vim.basic

update-alternatives --set vim /usr/bin/vim.basic

回显:

update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in manual mode

验证恢复:

vim --version | head -1

回显:

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Aug 24 2026 22:13:24)
ls -l /etc/alternatives/vim

回显:

lrwxrwxrwx 1 root root 18 Oct  6 06:41 /etc/alternatives/vim -> /usr/bin/vim.basic

💡 说明:修复命令的核心是 update-alternatives --set vim /usr/bin/vim.basic。若想恢复自动选择,可执行 update-alternatives --auto vim(回到 auto 模式,由 priority 决定);交互式选择可执行 update-alternatives --config vim 后输入对应序号。同理可管理 editor、view、vimdiff 各组。建议在 Neovim 与 Vim 并存的环境里明确各组指向,避免"输入 vim 却进入 Neovim"的困惑。

六、配置说明

6.1 配置文件位置与优先级

Neovim 的用户配置目录为 ~/.config/nvim/(可通过 $XDG_CONFIG_HOME 重定向)。0.6 版本支持的配置文件优先级(实测):

优先级文件说明
1~/.config/nvim/init.vimVimscript 配置,0.6.1 首选,实测最先加载(scriptnames 编号 1)
2~/.config/nvim/init.luaLua 配置,0.6 起支持;实测同样正常加载(init.vim 存在时 init.vim 优先)
3系统级/usr/share/nvim/sysinit.vim($VIM/sysinit.vim)

💡 说明:若 init.vim 与 init.lua 同时存在,实测 init.vim 生效(init.lua 不加载);只存在 init.lua 时,它作为唯一用户配置被加载。0.6.1 的 init.lua 只支持 0.6 时代的 Lua API(如 vim.opt、vim.api.nvim_*),与 0.8+ 的新 API(vim.keymap.set 等)不兼容,迁移时需注意。

6.2 init.vim 完整配置示例(原样)

以下为本文档实测使用的 ~/.config/nvim/init.vim 完整内容(四行,逐行照抄):

set number
set tabstop=4
set expandtab
set shiftwidth=4
cat ~/.config/nvim/init.vim

回显:

set number
set tabstop=4
set expandtab
set shiftwidth=4

💡 说明:set number 开启行号;set tabstop=4 一个 Tab 显示为 4 个空格宽度;set expandtab 输入 Tab 时插入空格而非制表符;set shiftwidth=4 缩进命令(>>/<<、自动缩进)的步长为 4。

6.3 验证 init.vim 生效

headless 下用 echo &选项 直接读取"经过配置加载后"的选项值:

nvim --headless -c 'echo &number' -c 'q'

回显:

1
nvim --headless -c 'echo &tabstop' -c 'q'

回显:

4

💡 说明:&number 为 1、&tabstop 为 4,与 init.vim 中 set number、set tabstop=4 完全对应,证明配置已生效。此方法适合在 CI 中自动校验配置正确性。

6.4 init.lua 在 0.6.1 的实测行为

0.6.1 支持 init.lua。把 init.vim 暂时移走、创建 init.lua 实测:

mv ~/.config/nvim/init.vim ~/.config/nvim/init.vim.bak
printf 'vim.opt.number = true\nvim.opt.tabstop = 4\n' > ~/.config/nvim/init.lua
cat ~/.config/nvim/init.lua

回显:

vim.opt.number = true
vim.opt.tabstop = 4

验证配置生效:

nvim --headless -c 'echo &number' -c 'q'

回显:

1
nvim --headless -c 'echo &tabstop' -c 'q'

回显:

4

查看加载顺序(第 1 行应为 init.lua):

nvim --headless -c 'scriptnames' -c 'q' | head -2

回显:

  1: ~/.config/nvim/init.lua
  2: /usr/share/nvim/runtime/filetype.vim

恢复 init.vim 配置:

rm ~/.config/nvim/init.lua
mv ~/.config/nvim/init.vim.bak ~/.config/nvim/init.vim

💡 说明:实测 0.6.1 的 init.lua 可以正常加载并生效(vim.opt 赋值可用),并非文档流传的"0.6 不支持 init.lua"。但需注意 0.6 的 Lua API 较旧:没有 vim.keymap.set、vim.diagnostic 等后续版本函数,旧插件(按 0.5/0.6 写的)兼容,新插件(按 0.8+ 写的)可能报 attempt to call field 'xxx' (a nil value),此时应把相关配置迁移回 init.vim 或升级 nvim。

6.5 无配置时的默认值对比

移走全部用户配置后,实测默认值:

nvim --headless -c 'echo &number' -c 'q'

回显:

0
nvim --headless -c 'echo &tabstop' -c 'q'

回显:

8

⚠️ 注意:无配置时 &number=0(不显示行号)、&tabstop=8(Tab 显示 8 空格,Vim 传统默认)。与 6.3 节对比即可确认配置是否生效。

七、常见问题(FAQ)

7.1 为什么 has("lua") 返回 0,但 :lua 能用?

Ubuntu 22.04 打包的 neovim 0.6.1-3 编译特性表只有 +acl +iconv +tui(nvim --version 的 Features 行无 +lua),因此 Vimscript 的 has("lua") 探测返回 0。但 LuaJIT 2.1.0-beta3 确实内嵌在二进制里,:lua print(1+1) 实测正常输出 2。判断 Lua 是否可用应直接运行 :lua 代码,或使用 luaeval('1+1');不要用 has("lua") 做依据。

7.2 为什么 v:version 是 800,而不是 0.6?

v:version 是 Neovim 为兼容 Vimscript 保留的"Vim 版本基线"字段,恒定报告 800(对应 Vim 8.0 特性级别),与 Neovim 自身版本无关。需要判断 nvim 版本时用 has("nvim-0.6")(实测返回 1)或直接看 nvim --version 第一行。

7.3 nvim +checkhealth 报 "Locale does not support UTF-8" 怎么办?

该错误来自未设置 LANG/LC_ALL/LC_CTYPE(实测本机三个变量均为空)。修复:在 /etc/environment 或 shell 配置中设置 LANG=C.UTF-8(或 en_US.UTF-8),重新登录后生效;若 localectl 可用,也可执行 sudo localectl set-locale LANG=en_US.UTF-8。另一个 terminal 项的 command failed: infocmp -L 说明缺 infocmp 工具,安装 sudo apt install -y ncurses-bin 即可。

7.4 出现 E325: ATTENTION ... Found a swap file 怎么办?

说明存在未清理的 swap 文件(崩溃或并发编辑遗留),路径类似 ~/.local/share/nvim/swap//%opt%...%demo.txt.swp。处理:先确认没有其他 nvim 实例在编辑该文件(ps -ef | grep nvim),确认无残留进程后删除 swap 文件,或用 nvim -r 文件 恢复未保存内容。也可以在启动时加 -n(不使用 swap)临时绕过。

7.5 -c 'lua ...' 报 E5107: Error loading lua 是什么原因?

实测示例:nvim --headless -c "lua vim.api.nvim_echo({{"hello from lua"}}, true, {})" -c 'q' 因 Lua 字符串的引号被外层 shell 截断,nvim 收到的代码变为 ...{{hello from lua}}...,报 E5107 ... '}' expected near '<eof>'。解决:Lua 字符串用单引号、外层 -c 参数用双引号(或反之),保证引号配对完整。

7.6 为什么 :reg 显示 clipboard: No provider?

服务器(无桌面环境)未安装剪贴板提供者。Neovim 与系统剪贴板交互需要 xclip/xsel(X11)或 wl-clipboard(Wayland)。需要剪贴板功能时安装 sudo apt install -y xclip,然后重试 :reg 或 "+p 等剪贴板寄存器操作;headless 脚本模式不使用剪贴板,可忽略此提示。

7.7 输入 vim 打开的是 Neovim,如何恢复?

这是 Ubuntu alternatives 接管问题(见 5.12 节实测):nvim 注册进了 vim 与 editor 组。执行 sudo update-alternatives --set vim /usr/bin/vim.basic 即可让 vim 命令恢复运行 Vim;同理 sudo update-alternatives --set editor /usr/bin/vim.basic 可把系统默认编辑器切回 Vim。交互式选择用 sudo update-alternatives --config vim。

八、卸载与清理

8.1 确认当前安装状态

dpkg -l neovim

回显:

Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version      Architecture Description
+++-==============-============-============-=================================
ii  neovim         0.6.1-3      amd64        heavily refactored vim fork

💡 说明:ii 表示 Desired=install(期望安装)且 Status=installed(已安装)。

8.2 卸载(保留配置文件)

sudo apt remove -y neovim

回显(末尾片段,实测包含 update-alternatives 的联动调整):

update-alternatives: warning: skip creation of /usr/share/man/da/man1/view.1.gz because associated file /usr/share/man/da/man1/vim.1.gz (of link group view) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/de/man1/view.1.gz because associated file /usr/share/man/de/man1/vim.1.gz (of link group view) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/fr/man1/view.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group view) doesn't exist
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode

💡 说明:卸载时 dpkg 会联动调整 alternatives:view/vimdiff 等链接组自动回退到 vim.basic,并跳过不存在的 man 页链接(warning 属正常现象)。卸载后确认:

dpkg -l neovim

回显:

||/ Name           Version      Architecture Description
+++-==============-============-============-=================================
un  neovim         <none>       <none>       (no description available)

💡 说明:un 表示 Desired=unknown、Status=not-installed(未安装),即已卸载。需要彻底清理配置文件与运行时数据时改用 sudo apt purge -y neovim(会删除 /usr/share/nvim 运行时与相关数据),用户配置 ~/.config/nvim/ 需手动删除:rm -rf ~/.config/nvim。卸载后再安装只需 sudo apt install -y neovim(实测重装后 nvim --version 恢复 NVIM v0.6.1)。

九、参考链接