在掌握了基本布局之后,本文将介绍 Zellij 的高级布局特性:交换布局(Swap Layouts)允许你在同一组面板之间快速切换不同的排列方式;布局内嵌配置让你把环境变量和全局选项直接写入布局文件;最后还会提供多个实用的布局示例供你直接使用。
一、交换布局(Swap Layouts)
交换布局允许你在不改变面板内容的前提下,快速切换面板的排列方式。这在不同的工作场景之间切换时非常有用——比如从"编辑+终端"布局切换到"全屏终端"布局。
创建 .swap.kdl 文件
交换布局文件与主布局文件同名,但扩展名为 .swap.kdl,放在同一个 layouts/ 目录下:
// ~/.config/zellij/layouts/dev.swap.kdl
swap_tiled_layout name="vertical" {
pane
pane
}
swap_tiled_layout name="horizontal" {
pane split_direction="horizontal"
pane split_direction="horizontal"
}
swap_tiled_layout name="fullscreen" {
pane
}
启动布局后,使用 Alt+[ 和 Alt+] 在定义的交换布局之间循环切换。
带约束的交换布局
交换布局支持面板数量约束,可以根据当前标签页中的面板数自动选择合适的布局:
// 当面板数最多为 2 时使用此布局
swap_tiled_layout name="pair" max_panes=2 {
pane size="50%"
pane size="50%"
}
// 当面板数最少为 3 时使用此布局
swap_tiled_layout name="triptych" min_panes=3 {
pane size="60%"
pane size="20%"
pane size="20%"
}
// 当面板数恰好为 4 时使用此布局
swap_tiled_layout name="quad" exact_panes=4 {
pane split_direction="vertical" {
pane
pane
}
pane split_direction="vertical" {
pane
pane
}
}
- max_panes:当面板数量不超过指定值时生效
- min_panes:当面板数量不少于指定值时生效
- exact_panes:当面板数量恰好等于指定值时生效
浮动面板交换布局
同样可以为浮动面板定义交换布局:
swap_floating_layout name="centered" {
floating_panes {
pane {
x "20%"
y "10%"
width "60%"
height "80%"
}
}
}
swap_floating_layout name="corner" {
floating_panes {
pane {
x "70%"
y "60%"
width "30%"
height "40%"
}
}
}
二、在布局中嵌入配置
布局文件支持嵌入全局配置和环境变量,这样布局文件就可以自包含,不需要依赖外部配置文件。
global_opts 块
使用 global_opts 块来设置 Zellij 的全局选项:
layout {
global_opts {
pane_frames false
mouse_mode true
scroll_buffer_size 10000
theme "catppuccin-mocha"
}
pane
pane
}
这些选项仅在加载该布局时生效,不会影响其他会话。常用的选项包括:
- pane_frames true/false:是否显示面板边框
- mouse_mode true/false:是否启用鼠标支持
- scroll_buffer_size:滚动缓冲区大小
- theme:使用的主题名称
env 块
使用 env 块来设置环境变量,这些变量会传递给布局中启动的所有进程:
layout {
env {
EDITOR "nvim"
PROJECT_ROOT "/home/user/my-project"
RUST_LOG "debug"
}
pane command="cargo" {
args "watch"
}
pane edit="src/main.rs"
}
环境变量在布局加载时设置,对面板中启动的命令和编辑器都有效。
三、实用布局示例
经典三面板布局
最常见的开发布局:左侧大面板用于编辑器,右上运行命令,右下查看日志。
// ~/.config/zellij/layouts/triple.kdl
layout {
pane split_direction="vertical" {
pane size="65%" focus=true
pane split_direction="horizontal" {
pane size="50%"
pane size="50%"
}
}
}
三面板 + 精简状态栏
在三面板布局底部添加一个无边框的状态栏面板:
// ~/.config/zellij/layouts/triple-status.kdl
layout {
pane split_direction="horizontal" {
pane split_direction="vertical" {
pane size="65%"
pane split_direction="horizontal" {
pane size="50%"
pane size="50%"
}
}
pane size=1 borderless=true {
command "bash"
args "-c" "while true; do echo \"$(date) | $(whoami) | $(pwd)\"; sleep 2; done"
}
}
}
快速项目浏览器
使用文件管理器快速浏览项目:
// ~/.config/zellij/layouts/project.kdl
layout {
pane size="30%" command="broot"
pane size="70%" focus=true
floating_panes {
pane {
x "60%"
y "70%"
width "35%"
height "25%"
command "git"
args "status"
}
}
}
Rust 项目开发布局
专为 Rust 项目设计的开发布局,包含编辑器、构建输出、测试和文档面板:
// ~/.config/zellij/layouts/rust-dev.kdl
layout {
env {
EDITOR "nvim"
}
tab name="代码" {
pane split_direction="vertical" {
pane size="60%" edit="src/main.rs" focus=true
pane split_direction="horizontal" {
pane size="50%" command="cargo" {
args "watch" "-x" "check"
}
pane size="50%" name="终端"
}
}
}
tab name="测试" {
pane command="cargo" {
args "test" "--" "--nocapture"
}
}
tab name="文档" {
pane command="cargo" {
args "doc" "--open"
}
}
floating_panes {
pane {
x "70%"
y "60%"
width "25%"
height "30%"
command "cargo"
args "clippy"
}
}
}
复杂布局示例(Zellij 开发布局)
这个示例展示了模板、插件和交换布局的综合使用,适合 Zellij 自身的开发环境:
// ~/.config/zellij/layouts/zellij-dev.kdl
layout {
global_opts {
pane_frames true
theme "default"
}
pane_template name="editor" {
pane edit={file} focus=true
}
pane_template name="runner" {
pane command={cmd} size="30%"
}
default_tab_template {
pane size="70%"
pane size=1 borderless=true {
command "bash"
args "-c" "git log --oneline -5"
}
}
tab name="编辑器" {
editor file="src/main.rs"
runner cmd="cargo" {
args "build"
}
}
tab name="终端" {
pane command="bash"
}
}
// ~/.config/zellij/layouts/zellij-dev.swap.kdl
swap_tiled_layout name="coding" max_panes=3 {
pane size="70%"
pane split_direction="horizontal" {
pane
pane
}
}
swap_tiled_layout name="review" min_panes=4 {
pane split_direction="vertical" {
pane split_direction="horizontal" {
pane
pane
}
pane split_direction="horizontal" {
pane
pane
}
}
}
swap_tiled_layout name="focus" exact_panes=1 {
pane
}
以上布局示例都可以直接复制到你的 ~/.config/zellij/layouts/ 目录下使用。建议从简单的三面板布局开始,逐步根据自己的工作流调整和扩展。