Doautocmd

可以自己创建一个新的 event,放在任意的位置。

Concealing

Vim 的这个机制允许更好看的展示。

Buffer

In fact, any buffer has two independent flags: loaded and listed. "Loaded and nolisted" is a typical value for special buffers such as "help", so they are visible but not normally shown by :ls;

Unloaded buffer

A buffer is loaded if the corresponding file is loaded into memory.

Unlisted buffer

set buftype? outputs empty.

Use ls! to see all unlisted buffers.

Unlisted buffers won't trigger BufEnter.

autocompletion - What is the difference between loaded and unloaded buffers? - Vi and Vim Stack Exchange

lua vim.api.nvim_create_buf(false, true) (listed, scratch)

Registers in Vim/Neovim

寄存器是存储文本的容器,可通过 " 寄存器名 + 操作 访问。也就是所有的寄存器都是以 " 开头的。

通用操作语法:

  • "x y:复制到寄存器 x(y 表示操作,如 yy 复制行);
  • "x p:粘贴寄存器 x 内容;
  • :reg x:查看寄存器 x 的内容。

以下是 Vim 和 Neovim 中寄存器机制的简明总结,涵盖核心概念、寄存器类型及异同点:

寄存器类型

  • 默认寄存器
寄存器 描述
"" 无名寄存器:默认存储最近删除/复制的内容(d, y, c 等操作)
"0 最近复制寄存器:仅存储 y(复制)操作的内容
  • 命名寄存器
格式 描述
"a-"z 用户自定义的 26 个寄存器(如 "ayy 复制行到 a 寄存器)
"A-"Z 大写字母寄存器:追加内容到对应小写寄存器(如 "Ayy 追加到 a
  • 系统剪贴板寄存器(需剪贴板工具支持)
寄存器 用途 系统依赖
"+ 系统剪贴板(Ctrl+C/Ctrl+V xclip/wl-copy/pbcopy
"* 主选择(鼠标中键粘贴) xclip/wl-copy
  • 特殊功能寄存器
寄存器 用途 示例
"_ 黑洞寄存器:删除时不保存内容 "_dd 删除行且不保存
"/ 搜索寄存器:存储最近搜索内容 :echo @/ 查看当前搜索词
": 命令寄存器:存储最近执行的命令 :echo @: 查看
"= 表达式寄存器:执行表达式并插入 "=6*7<CR>p 插入 42
"% 文件名寄存器:当前文件路径 :echo @% 显示文件名
". 最后插入文本:最近插入的文本 ".p 粘贴最后插入内容
  • 只读寄存器
寄存器 内容
"# 轮换文件名(alternate file)
"~ 最近拖拽/外部粘贴的文本(Neovim)

配置建议

  • 默认使用系统剪贴板(Neovim 推荐):
set clipboard=unnamedplus  " 复制/删除操作使用 `+` 寄存器
  • 兼容 Vim 行为
set clipboard=  " 禁用系统剪贴板集成(纯 Vim 模式)

系统剪切板 / clipboard provider

set clipboard=unnamedplus  " 复制/删除操作使用 `+` 寄存器

clipboard 变量的作用是指定无名寄存器应该和哪些寄存器(比如 * 或者 + 来绑定)。

如果设置了 unnamedplus,那么每次复制的时候应该同时复制到系统寄存器 unnamedplus。但是 Vim 没有办法直接连接到系统剪切板,只能通过一个 provider 通过 shell 的方式和系统剪切板通信。

g.clipboardopt.clipboard 的区别:

分别负责剪贴板底层实现和寄存器同步行为,两者是不同层级的配置。

  • vim.g.clipboard 定义如何调用外部工具(如 xclip);
  • vim.o.clipboard 控制无名寄存器是否与系统剪贴板同步。

clipboard provider 就是在 g.clipboard 中提供的,比如:

vim.g.clipboard = {
  name = 'myClipboard',
  copy = {
    -- 定义复制行为(支持多寄存器)
    ['+'] = 'xclip -selection clipboard',
    ['*'] = 'xclip -selection primary',
  },
  paste = {
    -- 定义粘贴行为(支持多寄存器)
    ['+'] = { 'xclip', '-selection', 'clipboard', '-o' },
    ['*'] = { 'xclip', '-selection', 'primary', '-o' },
  },
  -- 可选:缓存逻辑或条件判断
  cache_enabled = true
}

Neovim

Useful APIs of Neovim

Return a vimscript function call's result to lua

vim.fn.eval()

Get current buffer

vim.fn.bufnr()

Get current file name

lua print(vim.fn.expand("%:p"))
:help expand
:echo expand("%:p")    " absolute path
:echo expand("%:p:h")  " absolute path dirname
:echo expand("%:p:h:h")" absolute path dirname dirname
:echo expand("%:.")    " relative path
:echo expand("%:.:h")  " relative path dirname
:echo expand("%:.:h:h")" relative path dirname dirname

:echo expand("<sfile>:p")  " absolute path to [this] vimscript

:help filename-modifiers

How can I expand the full path of the current file to pass to a command in Vim? - Stack Overflow

Get current cursor position

local r,c = unpack(vim.api.nvim_win_get_cursor(0))
lua print(unpack(vim.api.nvim_win_get_cursor(0)))

Get current mode

vim.api.nvim_get_mode()

Get register content

vim.fn.getreg()

Send key stroke

vim.api.nvim_command([[ 
	call feedkeys("m")
]])

Get a buffer's filetype

vim.bo[bufnr].filetype

Get a buffer's path/name

vim.api.nvim_buf_get_name(buffer)

Change to a new buffer

won't trigger BufEnter

vim.api.nvim_win_set_buf

Neovim Plugins

Iron.nvim

Mappings                                                        *iron-mappings*

Iron by default doesn't map the keybindings, only those supplied in the
core.setup function.

- send_motion: Sends a motion to the repl
- visual_send: Sends the visual selection to the repl
- send_file: Sends the whole file to the repl
- send_line: Sends the line below the cursor to the repl
- send_mark: Sends the text within the mark
- mark_motion: Marks the text object
- mark_visual: Marks the visual selection
- remove_mark: Removes the set mark
- cr: Sends a return to the repl
- interrupt: Sends a `<C-c>` signal to the repl
- exit: Exits the repl
- clear: Clears the repl window