Tmux工作流快捷键配置

说明

以下只列出主要配置内容,完整可运行的配置见:

https://github.com/timothy020/shell_configuration

WezTerm配置

配置Session,Window,Pannel操作快捷键

  • Session:快速detach,退出,查询所有session信息
  • Window:快速创建,切换,删除窗口
  • Pannel:快速分割,切换panel

配置文件: .config/wezterm/wezterm.lua

  -- ╭─────────────────────────────────────────────────────────╮
  -- │                    tmux session操作                      │
  -- ╰─────────────────────────────────────────────────────────╯
  k.cmd_to_tmux_prefix("d", "d"), -- Cmd+d: detach当前会话
  k.cmd_to_tmux_prefix("e", "K"), -- Cmd+e: 退出当前会话(前提是在 ~/.tmux.conf配置了"bind K kill-session")
  k.cmd_to_tmux_prefix("a", "w"), -- Cmd+a: 打开tmux-sessionx会话管理器

  -- ╭─────────────────────────────────────────────────────────╮
  -- │                    tmux windows操作                      │
  -- ╰─────────────────────────────────────────────────────────╯
  k.cmd_to_tmux_prefix("n", "c"),  -- Cmd+n: 创建新的tmux窗口
  k.cmd_to_tmux_prefix("w", "&"),  -- Cmd+w: 关闭当前tmux窗口
  k.cmd_to_tmux_prefix("H", "p"),  -- Cmd+j: 切换到上一个窗口
  k.cmd_to_tmux_prefix("L", "n"),  -- Cmd+k: 切换到下一个窗口
  k.cmd_to_tmux_prefix("0", "0"),  -- Cmd+0: 切换到tmux窗口0
  k.cmd_to_tmux_prefix("1", "1"),  -- Cmd+1: 切换到tmux窗口1
  k.cmd_to_tmux_prefix("2", "2"),  -- Cmd+2: 切换到tmux窗口2
  k.cmd_to_tmux_prefix("3", "3"),  -- Cmd+3: 切换到tmux窗口3
  k.cmd_to_tmux_prefix("4", "4"),  -- Cmd+4: 切换到tmux窗口4
  k.cmd_to_tmux_prefix("5", "5"),  -- Cmd+5: 切换到tmux窗口5
  k.cmd_to_tmux_prefix("6", "6"),  -- Cmd+6: 切换到tmux窗口6
  k.cmd_to_tmux_prefix("7", "7"),  -- Cmd+7: 切换到tmux窗口7
  k.cmd_to_tmux_prefix("8", "8"),  -- Cmd+8: 切换到tmux窗口8
  k.cmd_to_tmux_prefix("9", "9"),  -- Cmd+9: 切换到tmux窗口9

  -- ╭─────────────────────────────────────────────────────────╮
  -- │                    tmux panel操作                        │
  -- ╰─────────────────────────────────────────────────────────╯
  k.cmd_to_tmux_prefix("p", '"'), -- Cmd+p: tmux水平分割面板
  k.cmd_to_tmux_prefix("P", "%"), -- Cmd+Shift+p: tmux垂直分割面板
  k.cmd_to_tmux_prefix("x", "x"), -- Cmd+x: 关闭当前tmux面板
  -- Cmd+h/l/j/k: 切换到左一个/右一个/上一个/下一个面板
  k.cmd_to_tmux_prefix("h", "LeftArrow"),
  k.cmd_to_tmux_prefix("l", "RightArrow"),
  k.cmd_to_tmux_prefix("j", "UpArrow"),
  k.cmd_to_tmux_prefix("k", "DownArrow"),

Tmux配置

  • 配置鼠标交互
  • 配置关闭Session 的快捷键
  • 配置关闭Pane和Window无需二次确认
  • 关闭Window后,序号自动重排序
  • 配置TPM和dracula主题

安装TPM和dracula主题

# 安装TPM
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# 安装dracula主题
git clone https://github.com/dracula/tmux.git ~/.tmux/plugins/tmux
# 彻底刷新 tmux 环境 
tmux kill-server && tmux

配置文件:~/.tmux.conf

# 开启鼠标交互
set -g mouse on
# 绑定 ctrl b + K 关闭session
bind K kill-session
# windows序号自动重排
set-option -g renumber-windows on
# 关闭分屏 (Pane) 时不提示确认,直接关闭
bind x kill-pane
# 关闭窗口 (Window) 时不提示确认,直接关闭
bind & kill-window

# 1. 设置 TPM 插件列表
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

# 2. 引入 dracula 主题插件
set -g @plugin 'dracula/tmux'

# Dracula 主题配置(可选:显示 CPU、内存、天气、时间)
set -g @dracula-plugins "cpu-usage ram-usage time"
set -g @dracula-show-powerline true    # 开启像箭头一样的分隔符
set -g @dracula-show-left-icon session # 左边显示 session 图标

# 3. 初始化 TPM (必须放在文件最后一行)
run '~/.tmux/plugins/tpm/tpm'

Zshell配置

配置alias简化tmux目录输入

  • tc:创建新session
  • tl:列出所有session
  • ta <序号[可选]> :attach到最新的session或指定session
  • t0:每次终端启动时进入最新的session,如果没有session,则创建一个

配置文件 ~/.zshrc

# tmux相关配置
alias tc="tmux"
alias tl="tmux ls"
ta() {
	if [ -z "$1" ]; then
    tmux attach
  else
    tmux attach -t "$1" "${@:2}"
  fi
}
tk() {
  if [[ -z "$1" ]]; then
    tmux kill-server
  else
    tmux kill-session -t "$1"
  fi
}
t0() {
  # 已在 tmux 内就不做任何事,避免嵌套
  [[ -n "$TMUX" ]] && return 0

  # 没有任何 session:tmux ls 会失败 -> 直接新建并进入
  tmux ls >/dev/null 2>&1 || { tmux new-session; return; }

  # 有 session:直接 attach(默认行为是 attach 到一个现有 session)
  tmux attach
}

# ...

# 放到最后,每次终端启动时启动tmux
t0

posted @ 2026-01-19 00:21  Miaops  阅读(0)  评论(0)    收藏  举报