安装 tmux
sh
sudo apt install tmux
cd ~
touch .tmux.conf
常用命令
sh
tmux new -s <session_id>
连接到指定 session:
sh
tmux attach -d -t <session_id>
连接到最近的 session:
sh
tmux attach
一键配置
sh
cp ~/.tmux.conf ~/.tmux.conf.bak
sh
wget https://raw.githubusercontent.com/Hansimov/blog/main/docs/notes/configs/.tmux.conf -O ~/.tmux.conf && tmux source ~/.tmux.conf
.tmux.conf 完整样例
conf
# TMUX Guide
# https://tmuxguide.readthedocs.io/en/latest/tmux/tmux.html#tmux-conf
# tmux(1) - Linux manual page
# https://man7.org/linux/man-pages/man1/tmux.1.html#FORMATS
# 256 Colors - Cheat Sheet - Xterm, HEX, RGB, HSL
# https://www.ditig.com/256-colors-cheat-sheet
# kill tmux process (if stuck) with following commands
# ps aux | grep tmux
# kill -9 <pid>
# bind keys
unbind C-b
set -g prefix M-z
bind M-z send-prefix
# reload config file
bind r source-file ~/.tmux.conf \; display ".tmux.conf reloaded!"
# enable mouse support
set -g mouse on
# set status style
set -g status-interval 1
set-option -g status-position bottom
set-option -g status-style bg=default
set-option -g status-left ""
set-option -g window-status-format ""
set -g window-status-current-format "#[fg=cyan,bold]#{pane_title} : [#{pane_current_path}]"
# set-option -g status-right "#[fg=cyan,bold] #(TZ='Asia/Beijing' date +'[ww')""#(expr #(TZ='Asia/Beijing' date +'%%U'))""#(TZ='Asia/Beijing' date +'.%%w] %%m-%%d %%H:%%M:%%S')"
# set-option -g status-right "#[fg=cyan,bold] #(date +'[ww')""#(date +'%U')""#(date +'.%w] %m-%d %H:%M:%S')" # In Fish Shell
set-option -g status-right "#[fg=cyan,bold] #(date +'[ww')""#(expr $(date +'%U') + 1)""#(date +'.%w] %m-%d %H:%M:%S')"
# [Tip] To reset set-option, use following cmd:
# set -gu <option>
# set pane border style
set -g pane-border-status top
set -g pane-border-style bg=default,fg=cyan
set -g pane-active-border-style bg=cyan,fg=black
# rename pane title
# tmux select-pane -t <pane-idx> -T <pane-title>
setw -g pane-border-format ' #{pane_index}: #{pane_title} -- [#{pane_current_path}] '
# start selection with 'space' and copy using 'y'
# bind -t vi-copy 'y' copy-selection
# paste using 'p'
# unbind p
# bind p paste-buffer
# bind keys for copy mode
unbind -n a
# bind-key -n M-a copy-mode
unbind-key -T root MouseDrag1Pane
unbind-key -T copy-mode-vi MouseDrag1Pane
unbind-key -T copy-mode MouseDrag1Pane
# bind-key -T copy-mode MouseDrag1Pane copy-selection -x
# bind-key -T copy-mode-vi MouseDrag1Pane copy-selection -x
# [Tip] Shift+LeftMouse select can copy text to clipboard,
# and Shift+Insert can paste from clipboard
# which zsh
set-option -g default-shell /usr/bin/zsh
# set buffer lines
set-option -g history-limit 5000
安装插件管理器 tpm
Tmux Plugin Manager
A list of tmux plugins.
下载代码仓库:
sh
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
在 .tmux.conf
末尾添加:
sh
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
重新加载配置:
sh
# type this in terminal if tmux is already running
tmux source ~/.tmux.conf
安装会话恢复插件 tmux-resurrect
tmux-resurrect: Persists tmux environment across system restarts.
Restoring programs
安装插件
首先确保已经安装了 tpm 插件管理器。
在 .tmux.conf
的插件列表中添加:
sh
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
按下插件管理器的安装快捷键:Prefix
+ I
。 等待一会,安装完成会提示按下 Esc
退出。
恢复程序
可以通过 .tmux.conf
中的 set -g @resurrect-processes '...'
命令来指定。不同程序之间用空格分隔。
- 插件默认恢复的程序:sh
vi vim nvim emacs man less more tail top htop irssi weechat mutt
- 指定额外恢复的程序:sh
set -g @resurrect-processes 'ssh psql mongosh elasticsearch'
- 带参数的程序需加上双引号:sh
set -g @resurrect-processes "kibana serve --host 0.0.0.0 --port 5601"
- 不恢复任何程序:sh
set -g @resurrect-processes 'false'
- 恢复所有程序:sh
set -g @resurrect-processes 'true:all'
保存和恢复会话
- 保存:
Prefix
+ctrl-s
- 恢复:
Prefix
+ctrl-r
配置示例
sh
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
# Restore programs
set -g @resurrect-processes '\
ssh psql mongosh \
"~elasticsearch->elasticsearch *" \
"~kibana serve->kibana serve *" \
"~python->python *" \
"~quasar dev->quasar dev *" \
"~./frpc->./frpc *" \
"~docker->docker *" \
'
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'