Skip to content

Instantly share code, notes, and snippets.

@lazydao
Last active August 5, 2026 09:03
Show Gist options
  • Select an option

  • Save lazydao/c6dc7f7e642a5402057af2cf032d3009 to your computer and use it in GitHub Desktop.

Select an option

Save lazydao/c6dc7f7e642a5402057af2cf032d3009 to your computer and use it in GitHub Desktop.
Tmux
# --- Terminal / truecolor ---
set -g default-terminal "tmux-256color"
# 注意:terminal-features 匹配的是【外层终端的 TERM】,不是上面的 default-terminal。
# TERM 不以 xterm 开头的终端(wezterm/foot/alacritty/ghostty)不会命中 xterm* 规则,
# 且 RGB 与 extkeys 会一并静默失效、无任何报错。故预先列出常见项。
set -as terminal-features ",xterm*:RGB:extkeys" # 含 xterm-256color / xterm-kitty / xterm-ghostty
set -as terminal-features ",alacritty*:RGB:extkeys"
set -as terminal-features ",wezterm*:RGB:extkeys"
set -as terminal-features ",foot*:RGB:extkeys"
set -as terminal-features ",contour*:RGB:extkeys"
# 嵌套 tmux,或外层 TERM 为 tmux-256color 时启用
# set -as terminal-features ",tmux*:RGB:extkeys"
# 仅供 pane 内的程序(nvim 等)识别真彩色,且只对【之后新建】的 pane 生效。
# tmux 自身是否按 RGB 渲染只取决于上面的 terminal-features,与此行无关。
set-environment -g COLORTERM truecolor
# --- Passthrough / clipboard ---
# allow-passthrough 是 pane 级选项:启动期加载配置时还没有 pane 上下文,
# 直接 `set -g` / `set -gp` 都会被静默忽略(实测 tmux 3.5a)。
# 用 run-shell -b 推迟到 server 就绪后执行,才真正生效。
#
# 安全面:开启后 pane 内【任意】程序都能把 OSC 直传给外层终端,其中包括
# OSC 52(写系统剪贴板)。cat 来路不明的文件、或 agent 输出里混入构造好的
# 序列,都可能改动剪贴板。若不需要图片/终端集成,注释掉此行是更安全的默认。
run-shell -b "tmux set -gp allow-passthrough on"
set -g set-clipboard on
# 更保守可用(只允许 tmux 自身发起剪贴板写入,不代理应用的 OSC 52):
# set -g set-clipboard external
# --- Keyboard ---
# 不加 -q:宁可让写错的选项报错,也别像 allow-passthrough 那样静默失效。
# 配置中某行报错不会中断后续行的加载(已实测),所以去掉 -q 无副作用。
set -g extended-keys on
set -g extended-keys-format csi-u
set -g focus-events on # report focus events
setw -g mode-keys vi # 进入复制模式的时候使用 vi 键位(默认是 EMACS)
# --- Index / mouse / panes ---
set -g base-index 1 # 窗口编号从 1 开始计数
set -g pane-base-index 1 # 窗格编号从 1 开始计数
set -g renumber-windows on # 关掉某个窗口后,编号重排
set -g display-panes-time 10000 # PREFIX-Q 显示编号的驻留时长,单位 ms
set -g mouse on # 开启鼠标
# --- Shell ---
set -g default-shell /bin/zsh # 默认 shell 设为 zsh
# --- Titles ---
set -g set-titles on
set -g set-titles-string "#{host_short} | #S | #I:#W"
set -g status-position top
# 禁止自动命名
setw -g automatic-rename off
setw -g allow-rename off
# --- Background Agent alerts ---
# 信号源用 bell,不用 activity。
# activity 的触发条件是【任何输出】:新窗口的 shell 打第一个 prompt、attach 时
# SIGWINCH 引起的 prompt 重绘,都会置位(实测:新建的空窗口立刻 activity=1)。
# 而该 flag 只在你切进那个窗口时才清除,所以长期 detach 的窗口会一直亮着。
#
# bell 则由 agent 在「跑完了 / 要我介入」时主动发出,语义精确。各 agent 的配置:
# Codex config.toml [tui] notifications + notification_method = "bel"
# Claude Code settings.json 的 preferredNotifChannel = "terminal_bell"
# opencode ~/.config/opencode/plugin/tmux-bell.js(它没有内置 bell,用插件补)
# monitor-bell 是全局 window 选项,新建 window/session 自动继承,无需按窗口手动开关。
setw -g monitor-activity off
setw -g monitor-bell on
set -g bell-action any # 任何窗口的 bell 都置标记
set -g visual-bell off # 不弹 message;bell 转发给外层终端,可让任务栏闪烁
set -g window-status-bell-style 'fg=colour16,bg=colour203,bold'
# --- Line drawing fallback: only enable if pane borders render badly ---
# set -as terminal-overrides ",*:U8=0"
# 避免 SSH 链路上 OSC 10/11 颜色响应被拆成普通输入。
# 代价:copy-mode(vi) 里按 ESC 退出有 100ms 可感延迟。
# 纯本地使用为主可降到 10~50;常走 SSH 则保持 100。
set -sg escape-time 100
# --- Layout snapshot ---
# 保存当前窗口布局到固定文件
bind-key S run-shell 'tmux display-message -p "#{window_layout}" > ~/.tmux-layout; tmux display-message "Saved layout to ~/.tmux-layout"'
# 从固定文件读取并应用布局
bind-key H run-shell 'layout="$(cat ~/.tmux-layout 2>/dev/null)"; if [ -n "$layout" ]; then tmux select-layout "$layout"; tmux display-message "Applied layout from ~/.tmux-layout"; else tmux display-message "No saved layout: ~/.tmux-layout"; fi'
#!/usr/bin/env zsh
set -euo pipefail
# codex remote-control: prefer the daemon path when the standalone install exists.
# Do not stop it here; that can disconnect active remote sessions.
CODEX_STANDALONE="${HOME}/.codex/packages/standalone/current/codex"
if [[ -x "${CODEX_STANDALONE}" ]]; then
if ! remote_control_output=$(zsh -lc 'source ~/.zshrc.local 2>/dev/null || true; codex remote-control start --json' 2>&1); then
print -u2 "warning: codex remote-control start failed"
print -u2 "${remote_control_output}"
if [[ "${remote_control_output}" == *"not managed by codex app-server daemon"* ]]; then
print -u2 "hint: an unmanaged app-server is already running; stop it once, then rerun codex remote-control start"
fi
fi
else
# Fallback for fnm/npm installs: `remote-control start` requires standalone Codex.
if ! tmux has-session -t codex-remote 2>/dev/null; then
tmux new-session -d -s codex-remote \
'zsh -c "source ~/.zshrc.local && TERM=xterm-256color codex remote-control"'
fi
fi
# H3 项目
SESSION="h3"
WORKDIR="/home/will/h3"
enter_tmux() {
if [[ -n "${TMUX:-}" ]]; then
tmux switch-client -t "${SESSION}"
else
tmux attach -t "${SESSION}"
fi
exit $?
}
if tmux has-session -t "${SESSION}" 2>/dev/null; then
enter_tmux
fi
tmux new-session -d \
-s "${SESSION}" \
-n codex \
-c "${WORKDIR}" \
'codex --yolo; exec zsh'
tmux new-window \
-t "${SESSION}" \
-n opencode \
-c "${WORKDIR}" \
'opencode; exec zsh'
tmux select-window -t "${SESSION}:codex"
enter_tmux

tmux 常见快捷键

默认前缀键为 Ctrl-b。下面的 Prefix 都表示先按 Ctrl-b,松开后再按后续按键。

会话

┌──────────────────────────┬────────────────────────────────┐ │ 快捷键 │ 作用 │ ├──────────────────────────┼────────────────────────────────┤ │ Prefix d │ 退出当前 tmux 会话但不关闭进程 │ │ tmux ls │ 查看已有会话 │ │ tmux attach -t │ 重新进入指定会话 │ │ tmux new -s │ 新建指定名称的会话 │ └──────────────────────────┴────────────────────────────────┘

窗口

┌────────────────────────────┬──────────────────┐ │ 快捷键 │ 作用 │ ├────────────────────────────┼──────────────────┤ │ Prefix c │ 新建窗口 │ │ Prefix , │ 重命名当前窗口 │ │ Prefix & │ 关闭当前窗口 │ │ Prefix n │ 切到下一个窗口 │ │ Prefix p │ 切到上一个窗口 │ │ Prefix <数字> │ 切到指定编号窗口 │ │ Prefix :swap-window -t 1 │ 交换当前和 1 号窗口 │ └────────────────────────────┴──────────────────┘

窗格

┌──────────┬─────────────────────┐ │ 快捷键 │ 作用 │ ├──────────┼─────────────────────┤ │ Prefix % │ 左右分屏 │ │ Prefix " │ 上下分屏 │ │ Prefix x │ 关闭当前窗格 │ │ Prefix o │ 切换到下一个窗格 │ │ Prefix q │ 显示窗格编号 │ │ Prefix z │ 当前窗格全屏/取消全屏 │ │ Prefix { │ 当前窗格向前换位 │ │ Prefix } │ 当前窗格向后换位 │ └──────────┴────────────────────┘

布局

┌──────────────┬───────────────────┐ │ 快捷键 │ 作用 │ ├──────────────┼───────────────────┤ │ Prefix Space │ 在内置布局之间切换 │ │ Prefix S │ 保存当前布局 │ │ Prefix H │ 应用保存的布局 │ └──────────────┴───────────────────┘

后台 Agent

┌────────────┬──────────────────────────────────────────┐ │ 快捷键 │ 作用 │ ├────────────┼──────────────────────────────────────────┤ │ Prefix M-s │ 开启当前窗口静默提醒(60s 无输出即提示) │ │ Prefix M-S │ 关闭当前窗口静默提醒 │ └────────────┴──────────────────────────────────────────┘

说明:窗口有输出时不提醒,安静超过 60s 才提示,用来判断 agent 是否已经跑完。 未开启的窗口不受影响,因此自己手打命令的窗口不会误报。

复制模式

当前配置使用 vi 键位。

┌──────────┬──────────────────────┐ │ 快捷键 │ 作用 │ ├──────────┼──────────────────────┤ │ Prefix [ │ 进入复制模式 │ │ q │ 退出复制模式 │ │ Space │ 开始选择 │ │ Enter │ 复制选择内容 │ │ Prefix ] │ 粘贴复制内容 │ │ / │ 在复制模式中向下搜索 │ └──────────┴──────────────────────┘

鼠标

当前已开启鼠标支持:

  • 点击切换窗口或窗格
  • 拖拽调整窗格大小
  • 滚轮查看历史输出
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment