Pipe from wezterm, kitty, and neovim terminals into your current neovim instance. Like `code -r` on steroids.
Access rate limit exceeded. Please log in to continue.
flatten.nvim
从 `:term`、Wezterm 和 Kitty 中打开文件和命令输出到当前 neovim 实例
https://github.com/willothy/flatten.nvim/assets/38540736/b4e4e75a-9be2-478d-9098-7e421dd6d1d9
演示配置见此处(写入时自动删除 gitcommit 和切换终端不是默认设置)
注意
Flatten 在发布稳定版 1.0.0 前正在进行一些不兼容的变更。
更多信息请参见 #87。
功能特性
安装[1]
需要 nvim >=0.10。
folke/lazy.nvim
require("lazy").setup({
{
"willothy/flatten.nvim",
config = true,
-- or pass configuration with
-- opts = { }
-- Ensure that it runs first to minimize delay when opening file from terminal
lazy = false,
priority = 1001,
},
--- ...
})
nvim-neorocks/rocks.nvim(通过 LuaRocks)
:Rocks install flatten.nvim
然后,在 plugins/flatten.lua 中:
require("flatten").setup({
-- your config
})
使用方法
正常打开文件:
nvim file1 file2
强制对文件进行阻塞:
# with a custom block handler, you can use `nvim -b file1 file2`
nvim --cmd 'let g:flatten_wait=1' file1
以差异模式打开文件:
nvim -d file1 file2
为 $VISUAL 启用阻塞模式:
# with a custom block handler, you can use `export VISUAL="nvim -b"`
export VISUAL="nvim --cmd 'let g:flatten_wait=1'" # allows edit-exec <C-x><C-e>
启用手册页格式:
export MANPAGER="nvim +Man!"
在打开文件前,在宿主实例中执行命令:
nvim --cmd <cmd>
在打开文件后,在主机实例中执行命令:
nvim +<cmd>
配置
默认值
Flatten 附带以下默认设置:
local flatten = require("flatten")
local config = {
hooks = {
should_block = flatten.hooks.should_block,
should_nest = flatten.hooks.should_nest,
pre_open = flatten.hooks.pre_open,
post_open = flatten.hooks.post_open,
block_end = flatten.hooks.block_end,
no_files = flatten.hooks.no_files,
guest_data = flatten.hooks.guest_data,
pipe_path = flatten.hooks.pipe_path,
},
block_for = {
gitcommit = true,
gitrebase = true,
},
disable_cmd_passthrough = false,
nest_if_no_args = false,
nest_if_cmds = false,
window = {
open = "current",
diff = "tab_vsplit",
focus = "first",
},
integrations = {
kitty = false,
wezterm = false,
},
}
常规设置
-
block_for:table<string, boolean>- 默认仅包含
gitcommit和gitrebase。 - 添加文件类型,当打开该类型文件时始终阻止 guest 实例。
- 默认仅包含
-
nest_if_no_args:boolean(默认:false)- 若为
true,当nvim未传入参数时将进行嵌套。
- 若为
-
nest_if_cmds:boolean(默认:false)- 若为
true,即使向nvim传递了命令,无参数时也会进行嵌套。
- 若为
-
disable_cmd_passthrough:boolean(默认:false)- 若为
true,将不会把命令传递给 host 实例。
- 若为
集成设置
-
integrations.wezterm:boolean(默认:false)- 若为
true,在相同工作目录下运行的 [Wezterm] 标签页将被合并到同一个 Neovim 实例中。
- 若为
-
integrations.kitty:boolean(默认:false)- 若为
true,[Kitty] 标签页将被合并到同一个 Neovim 实例中。 - Kitty 暂不支持按当前工作目录合并,欢迎提交 PR。
- 若为
窗口设置
-
window.open:"current"|"alternate"|"split"|"vsplit"|"tab"|"smart"|Flatten.OpenHandler- 默认值为
"alternate"。 "alternate":在交替窗口(<C-w>p,:h winnr())中打开文件。"current":在当前窗口中打开文件。"vsplit":在垂直分屏中打开文件。"split":在水平分屏中打开文件。"tab":在新标签页中打开文件。"smart":自动在交替窗口、可用的其他窗口以及新分屏之间选择。Flatten.OpenHandler:fun(opts: Flatten.OpenContext): window, buffer?- 自定义函数,返回窗口编号以及可选的缓冲区。
- 默认值为
-
window.diff:"split"|"vsplit"|"tab_split"|"tab_vsplit"|Flatten.OpenHandler-
默认值为
"tab_vsplit"。 -
"split":在水平分屏中打开文件。 -
"vsplit":在垂直分屏中打开文件。 -
"tab_split":在新标签页中打开文件。 -
"tab_vsplit":在新标签页中打开文件。 -
Flatten.OpenHandler:fun(opts: Flatten.OpenContext): window, buffer?- 自定义函数,返回窗口编号以及可选的缓冲区。
-
Flatten.OpenContext:files:string[]- 传递给 host 实例的文件列表。
argv:string[]- 来自 guest 实例的完整 argv 列表。
stdin_buf:Flatten.BufInfo?- 若创建了标准输入缓冲区,则包含其相关信息。
guest_cwd:string- guest 实例的当前工作目录。
data:any- 通过
guest_data钩子传递给 host 实例的数据。
- 通过
-
Flatten.BufInfo:fname:stringbufnr:integer
-
-
window.focus:"first"|"last"- 默认值为
"first"。
- 默认值为
钩子
默认值位于 flatten.hooks 中。
-
hooks.should_block:fun(argv: string[]): boolean- 如果访客应等待主机关闭文件,应返回
true。
- 如果访客应等待主机关闭文件,应返回
-
hooks.should_nest:fun(host: channel): boolean- 如果访客不应合并到与主机相同的 Neovim 实例中,应返回
true。 这对于自定义文件何时应发送到主机实例以及何时应在新实例中打开非常有用。
- 如果访客不应合并到与主机相同的 Neovim 实例中,应返回
-
hooks.pre_open:fun(opts: Flatten.PreOpenContext)-
在打开文件前调用。
-
Flatten.PreOpenContext:data:any- 从
guest_data钩子传递给主机的数据。
- 从
-
-
hooks.post_open:fun(opts: Flatten.PostOpenContext)-
在打开文件后调用。
-
Flatten.PostOpenContext:bufnr:integer- 已打开文件的缓冲区编号。
winnr:integer- 打开文件的窗口。
filetype:string- 已打开文件的文件类型。
is_blocking:boolean- 访客在主机编辑时是否会被阻塞。
is_diff:boolean- 文件是否在差异模式下打开。
data:any- 从
guest_data钩子传递给主机的数据。
- 从
-
-
hooks.block_end:fun(opts: Flatten.BlockEndContext)-
当主机关闭文件时调用。
-
Flatten.BlockEndContext:filetype:string- 已打开文件的文件类型。
data:any- 从
guest_data钩子传递给主机的数据。
- 从
-
-
hooks.no_files:fun(opts: Flatten.NoFilesArgs): Flatten.NoFilesBehavior-
当没有文件传递给访客实例时调用,用于确定如何处理。
-
Flatten.NoFilesArgs:argv:string[]
-
Flatten.NoFilesBehavior:boolean|{ nest: boolean, block: boolean }
-
-
hooks.guest_data:fun(): any- 当访客向主机发送数据时调用,允许将自定义数据传递给主机。
-
hooks.pipe_path:fun(): string- 用于确定某个实例是主机,还是应连接到主机的访客。
高级配置示例
Toggleterm
如果你使用可切换的终端,并且不希望新缓冲区在当前窗口中打开,你可以使用 alternate 模式代替 current 模式,从而在最后一个窗口中打开。通过这种方法,终端无需像旧示例配置那样关闭后重新打开。
“alternate” 模式未设为默认值的唯一原因是为了避免破坏用户现有的配置。如果用户有相关需求(例如,如果你希望将其设为默认值,请提交 issue,或在已有 issue 下评论),它可能会在未来某个时候成为默认模式。
请注意,当以阻塞模式打开文件(如 git commit)时,终端将无法访问。你可以从 post_open 钩子的 bufnr 或 filetype 参数中获取文件类型,以便仅针对阻塞文件关闭终端,并使用 block_end 钩子在之后重新打开终端。
以下是我针对 toggleterm 的设置,包括一个自动命令,用于在写入时自动关闭 git commit 缓冲区:
local flatten = {
"willothy/flatten.nvim",
opts = function()
---@type Terminal?
local saved_terminal
return {
window = {
open = "alternate",
},
hooks = {
should_block = function(argv)
-- Note that argv contains all the parts of the CLI command, including
-- Neovim's path, commands, options and files.
-- See: :help v:argv
-- In this case, we would block if we find the `-b` flag
-- This allows you to use `nvim -b file1` instead of
-- `nvim --cmd 'let g:flatten_wait=1' file1`
return vim.tbl_contains(argv, "-b")
-- Alternatively, we can block if we find the diff-mode option
-- return vim.tbl_contains(argv, "-d")
end,
pre_open = function()
local term = require("toggleterm.terminal")
local termid = term.get_focused_id()
saved_terminal = term.get(termid)
end,
post_open = function(bufnr, winnr, ft, is_blocking)
if is_blocking and saved_terminal then
-- Hide the terminal while it's blocking
saved_terminal:close()
else
-- If it's a normal file, just switch to its window
vim.api.nvim_set_current_win(winnr)
-- If we're in a different wezterm pane/tab, switch to the current one
-- Requires willothy/wezterm.nvim
require("wezterm").switch_pane.id(
tonumber(os.getenv("WEZTERM_PANE"))
)
end
-- If the file is a git commit, create one-shot autocmd to delete its buffer on write
-- If you just want the toggleable terminal integration, ignore this bit
if ft == "gitcommit" or ft == "gitrebase" then
vim.api.nvim_create_autocmd("BufWritePost", {
buffer = bufnr,
once = true,
callback = vim.schedule_wrap(function()
vim.api.nvim_buf_delete(bufnr, {})
end),
})
end
end,
block_end = function()
-- After blocking ends (for a git commit, etc), reopen the terminal
vim.schedule(function()
if saved_terminal then
saved_terminal:open()
saved_terminal = nil
end
end)
end,
},
}
end,
}
管道路径
Flatten 现在默认会检查 kitty 和 wezterm,其工作原理如下。
如果您使用其他终端模拟器或多路复用器,可以基于此实现您的 pipe_path 函数。
local pipe_path = function()
-- If running in a terminal inside Neovim:
if vim.env.NVIM then
return vim.env.NVIM
end
-- If running in a Kitty terminal,
-- all tabs/windows/os-windows in the same instance of kitty
-- will open in the first neovim instance
if vim.env.KITTY_PID then
local addr = ("%s/%s"):format(
vim.fn.stdpath("run"),
"kitty.nvim-" .. vim.env.KITTY_PID
)
if not vim.uv.fs_stat(addr) then
vim.fn.serverstart(addr)
end
return addr
end
end
关于
此名称的灵感来源于 Rust(或许还有其他语言?)中的 flatten 函数,该函数可展平嵌套类型(例如 Option<Option<T>> -> Option<T>)。
本插件的设计灵感源自 nvim-unception,二者目标一致,但实现方式略有不同,且本插件支持更多配置选项。
不建议对本插件进行延迟加载——flatten 应尽早加载。主机的启动过程基本没有额外开销(除了 setup() 函数),因为它利用了 Neovim 初始化时启动的 RPC 服务器。在 guest 会话中先加载其他插件只会导致性能下降。 ↩︎
Introduction
Pipe from wezterm, kitty, and neovim terminals into your current neovim instance. Like `code -r` on steroids.
Customize your domain