Post

一文看懂tmux

一文看懂tmux

tmux cheatsheet

安装与配置

1
2
3
4
5
6
7
8
# macOS
brew install tmux

# Ubuntu/Debian
sudo apt install tmux

# 创建和编辑配置文件
vim ~/.tmux.conf

核心概念

概念描述
会话Session最高层级容器(包含多个窗口)
窗口Window会话中的标签页(包含多个窗格)
窗格Pane窗口中的分割区域

常用配置项

1
2
3
4
5
6
7
8
9
10
11
# ~/.tmux.conf
set -g prefix C-a        # 修改前缀键为 Ctrl+a,默认为Ctrl+b
set -g mouse on          # 启用鼠标支持
set -g base-index 1      # 窗口编号从1开始
setw -g pane-base-index 1 # 窗格编号从1开始
set -g status-interval 1 # 状态栏刷新间隔
set -g history-limit 10000 # 增加历史命令缓存条数

# 状态栏主题
set -g status-bg black
set -g status-fg white

插件管理(TPM)

1
2
3
4
5
6
7
8
9
10
11
# 安装插件管理器
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

# 添加插件配置
# ~/.tmux.conf
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-battery'
set -g @plugin 'tmux-plugins/tmux-resurrect' # 会话保存/恢复

# 初始化插件
run '~/.tmux/plugins/tpm/tpm'

基础操作

会话管理

1
2
3
4
5
6
7
8
tmux new -s mysession    # 创建一个名为mysession的新会话
tmux ls                  # 列出所有会话
tmux detach              # 分离当前会话,也可以使用下面的快捷键
tmux attach -t mysession # 重新连接指定会话
tmux attach              # 重新连接上一个会话
tmux kill-session -t session_name # 删除指定会话
tmux kill-server                  # 删除所有会话
tmux switch -t session_name       # 切换到指定会话
快捷键功能
前缀键 + d分离当前会话
前缀键 + s显示所有会话
前缀键 + $重命名当前会话
前缀键 + :进入命令模式
前缀键 + ?查看快捷键帮助

窗格(pane)管理

1
2
3
tmux split-window               # 划分为上下两个窗格, -h 可以进行水平分割
tmux select-pane -U/-D/-L/-R    # 切换到不同的窗格
tmux swap-pane -U/-D            # 交换窗格的位置
快捷键功能
前缀键 + %垂直分割窗格
前缀键 + “水平分割窗格
前缀键 + 方向键切换窗格
前缀键 + z最大化当前窗格
前缀键 + x关闭当前窗格
前缀键 + q显示窗格编号
前缀键 + Space切换窗格布局
前缀键 + {与上一个窗格交换位置
前缀键 + }与下一个窗格交换位置

窗口(window)操作

1
2
3
tmux new-window -t name           # 创建一个新窗口
tmux select-window -t name/No     # 切换到指定窗口
tmux rename-window name           # 为窗口重命名,也可以使用下面的快捷键
快捷键功能
前缀键 + c创建新窗口
前缀键 + n切换到下一个窗口
前缀键 + p切换到上一个窗口
前缀键 + 数字跳转到指定编号窗口
前缀键 + &关闭当前窗口
前缀键 + ,重命名当前窗口
前缀键 + w列出所有窗口

复制模式

快捷键功能
前缀键 + [进入复制模式
方向键选择文本
空格键开始选中
Enter复制选中内容
前缀键 + ]粘贴
q退出复制模式

和系统剪贴板集成(Linux、macOS)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 启用默认剪贴板集成
set -g set-clipboard on

# 使用 reattach-to-user-namespace 包装默认 shell(根据你的 shell 调整)
set -g default-command "reattach-to-user-namespace -l zsh"  # zsh 用户
# set -g default-command "reattach-to-user-namespace -l bash"  # bash 用户

# 设置复制模式快捷键(以 vi 模式为例)
setw -g mode-keys vi

# 绑定快捷键:复制选中内容到系统剪贴板(Enter 键确认复制)
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "pbcopy"

# 启用 TPM 和插件
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-yank'

# 可选:设置 tmux-yank 使用系统剪贴板
set -g @yank_with_mouse on  # 允许鼠标选中直接复制到系统剪贴板
set -g @yank_selection 'clipboard'  # 同步到系统剪贴板

# 初始化 TPM
run '~/.tmux/plugins/tpm/tpm'

高级技巧

会话组

1
2
3
4
5
6
# 创建会话组
tmux new-session -s dev
tmux new-session -t dev -s debug

# 同步输入
前缀键 + :setw synchronize-panes

结对编程

1
2
3
4
5
6
# 创建共享会话
tmux -S /tmp/pair new -s pair_programming
chmod 777 /tmp/pair  # 设置权限

# 其他用户连接
tmux -S /tmp/pair attach

脚本自动化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
SESSION="dev"

# Create a new session
tmux new-session -d -s $SESSION

# Split window horizontally
tmux split-window -h

# Select the first pane and run a command
tmux select-pane -t 0
tmux send-keys "cd ~/projects" C-m

# Select the second pane and run another command
tmux select-pane -t 1
tmux send-keys "top" C-m

# Attach to the session
tmux attach-session -t $SESSION

最佳实践

  1. 合理使用窗格:单个窗口内不要超过4个窗格
  2. 命名规范:
    • 会话:项目名-环境(如:webapp-prod)
    • 窗口:服务类型(如:editor、logs、database)
  3. 会话持久化:
    1. 插件自动保存: tmux-resurrect
    2. 手动保存: prefix + Ctrl-s
  4. 快捷键优化:
    1. 将常用操作绑定到易达组合键
    2. 保持与Vim/其他工具快捷键一致
  5. 资源监控:在状态栏显示系统信息set -g status-right "#(uptime | cut -d ',' -f 3-)"
This post is licensed under CC BY 4.0 by the author.