tmux 入门
入门教程请参考:
进阶
以前折腾tmux了很多特性,经过最近这几年的使用,留下了使用频率最高、最有用的功能配置。
主要包括:
简化命令
安装oh-my-zsh, .zshrc
中配置plugins 中追加 tmux,
等同于:1
2
3
4
5
6
7ta='tmux attach -t'
tad='tmux attach -d -t'
tkss='tmux kill-session -t'
tksv='tmux kill-server'
tl='tmux list-sessions'
tmux=_zsh_tmux_plugin_run
ts='tmux new-session -s
tmux中使用vim快捷键
修改.tmux.conf
:1
2
3
4
5
6
7
8
9
10
11set-window-option -g mode-keys vi
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
split-window -c "#{pane_current_path}"
bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
bind c new-window -c "#{pane_current_path}"
- 配置vim的方向键
hjkl
- 来切换panel
C + b + [h/j/k/l]
- 分屏与新建窗口:
C + b + “
: 上下分屏C + b + %
: 左右分屏C + b + c
: 新建窗口
会话保存与恢复
如果你会使用tmux打开多个session,但又不想恩电脑重启,会话没有保存时,这个功能就很有用。
安装tmux 包管理软件:
1
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
配置自动保存,
.tmux.conf
中添加:1
2
3
4
5
6
7
8
9
10
11# List of plugins
set -g @tpm_plugins ' \
tmux-plugins/tpm \
tmux-plugins/tmux-continuum \
tmux-plugins/tmux-resurrect \
'
run-shell '~/.tmux/plugins/tpm/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @continuum-save-interval '1440'
其中,continuum-save-interval
为设置自动保存频率为一天一次,根据我以往使用经验,continuum-save-interval
设置太小时,频繁的保存动作会导致tmux使用卡顿。
重载配置, 使用其生效
1
tmux new -d -s delete-me && tmux run-shell ~/.tmux/plugins/tpm/scripts/install_plugins.sh && tmux source-file ~/.tmux.conf && tmux kill-session -t delete-me
会话快速恢复
虽然continuum-restore
选项支持自动恢复会话,但由于其只支持打开一个session时才创建,也就是为了恢复,你必须要创建一个临时的session,恢复完会话后,才删除。
所以以下是一个alias,用起来更方便一点, 请直接添加到 你的.zshrc
:
1 | alias ,tmuxr='tmux new -d -s delete-me && tmux run-shell ~/.tmux/plugins/tmux-resurrect/scripts/restore.sh && tmux kill-session -t delete-me && tmux attach || tmux attach' |
下次你重启电脑后,只需要 执行下,tmuxr
自动恢复会话
Tips
如果因为未知原因,之前的会话没有保存下来,可以去~/.tmux/resurrect
目前,手动建立软连接到last
文件1
ln -s tmux_resurrect_2020-02-26T15:27:02.txt last
Ubuntu环境快速安装
如果是在ubuntu环境,可以fork mydots,快速安装zsh, tmux, docker等基本工具