Tmux 终端复用器配置指南
安装 Tmux
官方网站
安装命令
# Ubuntu/Debian
sudo apt install -y tmux
# CentOS/RHEL
sudo yum install tmux
# Arch Linux
sudo pacman -S tmux
# macOS
brew install tmux
验证安装
tmux -V
# 输出类似:tmux 3.3a
安装 Tmux 插件管理器 (TPM)
官方网站
https://github.com/tmux-plugins/tpm
安装 TPM
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
创建 .tmux.conf 配置文件
这是我的个人 Tmux 配置文件:
### tmux.conf
# system setting
set -g default-terminal "tmux-256color"
set -g mouse on
# rebind prefix key
unbind C-b
set -g prefix 'C-d'
set -g mode-keys vi
# reload configuration
# bind R source-file ~/.tmux.conf \; display '~/.tmux.conf Reloaded!'
# windows behaver
set -g base-index 1
setw -g pane-base-index 1
set -g renumber-windows on
#setw -g automatic-rename on
# display options
set -g set-titles on
set -g display-panes-time 2000
# window navigation
unbind n
unbind p
unbind x
bind -n C-o previous-window
bind -n C-p next-window
bind -n C-n new-window -c "#{pane_current_path}"
bind -n C-q kill-pane
# split windows
unbind %
unbind \"
bind l split-window -h -c "#{pane_current_path}"
bind k split-window -v -c "#{pane_current_path}"
bind h split-window -hb -c "#{pane_current_path}"
bind j split-window -vb -c "#{pane_current_path}"
# windows size adjusting
bind -n M-f resize-pane -Z
bind -n M-j resize-pane -U 5
bind -n M-k resize-pane -D 5
bind -n M-h resize-pane -L 5
bind -n M-l resize-pane -R 5
# cursor-moving setting
unbind o
unbind \;
bind -T root C-Left select-pane -L
bind -T root C-Right select-pane -R
bind -T root C-Up select-pane -U
bind -T root C-Down select-pane -D
# pane-moving setting
bind < swap-pane -U
bind > swap-pane -D
# tmux plugins
run '~/.tmux/plugins/tmux/catppuccin.tmux'
set -g @catppuccin_flavor 'frappe' # latte | frappe | mocha | macchiato
set -g @catppuccin_window_status_style "rounded" # basic | rounded | slanted | custom | none
set -g @catppuccin_window_number_color "#{@thm_fg}"
set -g @catppuccin_window_current_number_color "#{@thm_green}"
set -g status-left ""
set -g status-right ""
set -g status-right-length 100
set -g @yank_action 'copy-pipe' # or 'copy-pipe-and-cancel' for the default
set -g status-right "#{net_speed} "
set -g @net_speed_interfaces "enp2s0 wlp3s0"
set -g @net_speed_format "%8s %8s "
set -agF status-right "#{E:@catppuccin_status_cpu}"
set -agF status-right "#{E:@catppuccin_status_weather}"
set-option -g @tmux-weather-interval 10
set -gq @catppuccin_weather_color "#ea999c"
set-option -g @tmux-weather-location "BeiJing"
set -ag status-right "#{E:@catppuccin_status_date_time}"
set -g "@catppuccin_date_time_text" " %m-%d %H:%M"
# set -ag status-right "#{E:@catppuccin_status_session}"
# set -ag status-right "#{E:@catppuccin_status_application}"
# set -ag status-right "#{E:@catppuccin_status_gitmux}"
# set -ag status-right "#{E:@catppuccin_status_load}"
# set -ag status-right "#{E:@catppuccin_status_pomodoro_plus}"
# set -ag status-right "#{E:@catppuccin_status_kube}"
# set -ag status-right "#{E:@catppuccin_status_up_time}"
# set -ag status-right "#{E:@catppuccin_status_user}"
# set -g status-right '#[fg=#{@thm_crust},bg=#{@thm_teal}] : #S '
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-cpu'
set -g @plugin 'tmux-plugins/tmux-net-speed'
set -g @plugin 'xamut/tmux-weather'
set -g @plugin 'catppuccin/tmux#v2.1.1'
run '~/.tmux/plugins/tpm/tpm'
配置文件说明
- 基础设置:修改前缀键、启用鼠标支持等
- 主题配置:设置状态栏样式和颜色
- 插件配置:定义要安装的插件列表
- 快捷键映射:自定义常用操作的快捷键
安装 Nerd Fonts 字体
官方网站
方法一:使用官方脚本安装
经过多次尝试,Meslo 字体表现最佳:
git clone --depth 1 https://github.com/ryanoasis/nerd-fonts.git && \
cd nerd-fonts/ && \
./install.sh Meslo
方法二:使用简化脚本安装(推荐)
wget https://raw.githubusercontent.com/mcarvalho1/Simple-NerdFonts-Downloader/c7854dae2153aa199277926bed4b992488b65a3d/nf_downloader.sh
注意:
nf_downloader.sh默认会安装所有字体,建议修改脚本只安装需要的字体。字体将保存在~/.local/share/fonts目录。
Tmux 基本使用
启动 Tmux
tmux
# 或指定会话名称
tmux new -s mysession
常用快捷键
| 快捷键 | 功能 |
|---|---|
Ctrl+b % | 垂直分屏 |
Ctrl+b " | 水平分屏 |
Ctrl+b 方向键 | 切换窗格 |
Ctrl+b c | 新建窗口 |
Ctrl+b n/p | 切换窗口 |
Ctrl+b d | 分离会话 |
Ctrl+b [ | 进入复制模式 |
Ctrl+b ] | 粘贴 |
会话管理
# 列出所有会话
tmux ls
# 附加到会话
tmux attach -t 会话名
# 重命名会话
tmux rename-session -t 旧名称 新名称
# 杀死会话
tmux kill-session -t 会话名
插件推荐
1. tmux-sensible
合理的默认配置
set -g @plugin 'tmux-plugins/tmux-sensible'
2. tmux-prefix-highlight
高亮显示前缀键状态
set -g @plugin 'tmux-plugins/tmux-prefix-highlight'
3. tmux-resurrect
会话保存和恢复
set -g @plugin 'tmux-plugins/tmux-resurrect'
4. tmux-continuum
自动保存会话
set -g @plugin 'tmux-plugins/tmux-continuum'
5. tmux-yank
系统剪贴板集成
set -g @plugin 'tmux-plugins/tmux-yank'
插件安装步骤
1. 添加插件到配置
在 .tmux.conf 中添加插件:
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-yank'
# 更多插件...
2. 初始化 TPM
在配置文件末尾添加:
# 初始化 TPM
run '~/.tmux/plugins/tpm/tpm'
3. 安装插件
- 重新加载配置文件:
tmux source ~/.tmux.conf - 按
Prefix + I(大写 i)安装插件
4. 更新插件
按 Prefix + U 更新所有插件
5. 卸载插件
- 从配置文件中删除插件行
- 按
Prefix + alt + u卸载插件
高级配置
1. 主题定制
# 设置状态栏样式
set -g status-style "fg=#665c54"
set -g window-status-current-style "fg=#ea6962"
# 设置面板边框
set -g pane-border-style "fg=#3c3836"
set -g pane-active-border-style "fg=#ea6962"
2. 鼠标支持
# 启用鼠标
set -g mouse on
# 鼠标滚轮支持
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'copy-mode -e'"
3. 复制模式优化
# 使用 vi 键绑定
setw -g mode-keys vi
# 复制到系统剪贴板
bind -T copy-mode-vi v send -X begin-selection
bind -T copy-mode-vi y send -X copy-pipe-and-cancel 'xclip -in -selection clipboard'
性能优化
1. 减少重绘
# 延长状态栏更新间隔
set -g status-interval 5
# 禁用不必要的重绘
set -g focus-events off
2. 内存优化
# 限制历史记录大小
set -g history-limit 10000
# 禁用会话恢复的自动保存
set -g @continuum-restore 'off'
故障排除
问题 1:插件不生效
# 检查 TPM 安装
ls ~/.tmux/plugins/tpm
# 重新安装插件
tmux kill-server
tmux
# 然后按 Prefix + I
问题 2:字体显示异常
# 清除字体缓存
fc-cache -fv
# 检查字体是否安装成功
fc-list | grep Meslo
问题 3:快捷键冲突
# 修改前缀键
set -g prefix C-a
unbind C-b
bind C-a send-prefix
最佳实践
1. 配置文件管理
# 将配置文件纳入版本控制
git init ~/.tmux
git add .tmux.conf
git commit -m "Initial tmux configuration"
2. 会话命名规范
# 使用有意义的会话名称
tmux new -s web-dev
tmux new -s database
tmux new -s monitoring
3. 备份和恢复
# 手动备份会话
tmux list-sessions > tmux-sessions-backup.txt
# 使用插件自动备份
# 启用 tmux-resurrect 和 tmux-continuum
Tmux 是一个强大的终端复用工具,通过合理配置可以显著提高命令行工作效率。花时间学习和定制 Tmux,你会发现它成为日常开发中不可或缺的工具。