tmux 使用
常用
tmux(terminal multiplexer)是一个终端多路复用器,它允许你在一个终端会话中运行多个终端会话,并在不同的 SSH 会话之间保持连接。
-
启动 tmux 会话:
tmux new -s mysession这里
-s mysession是可选的,用于命名你的会话。 -
分离(detach)会话:
在
tmux会话中,按下Ctrl-b然后按d,这会将你从tmux会话中分离出来,而会话继续在后台运行。 -
列出
tmux会话:tmux ls -
重新连接到 tmux 会话:
tmux a -t mysession -
关闭会话:
可以在
tmux会话中输入exit来关闭会话,或者在tmux外部使用:tmux kill-session -t mysession
快捷键
所有快捷键都需要按下前缀 Ctrl-b 后使用。
- 分离窗口:
d - 创建新窗口:
c - 切换窗口:
n切换到下一个窗口,p切换到上一个窗口。 - 分割窗格:
%垂直分割窗格,"水平分割窗格。 - 切换到下一个窗格:
o
配置文件
~/.tmux.conf:
set -g mouse on # 启用鼠标支持
set -g history-limit 50000 # 扩展 tmux 历史
set -g default-terminal "tmux-256color" # 设置默认终端模拟器
set -g default-shell "/usr/bin/zsh" # 设置默认 shell
Oh my tmux!
可以使用 Oh my tmux! 美化 tmux 界面。
安装
git clone --single-branch https://github.com/gpakosz/.tmux.git ~/.tmux
ln -s ~/.tmux/.tmux.conf ~
cp ~/.tmux/.tmux.conf.local ~
启用 Powerline 字体:
sed -e "s/^tmux_conf_theme_left_separator_main/#tmux_conf_theme_left_separator_main/" \
-e "s/^tmux_conf_theme_left_separator_sub/#tmux_conf_theme_left_separator_sub/" \
-e "s/^tmux_conf_theme_right_separator_main/#tmux_conf_theme_right_separator_main/" \
-e "s/^tmux_conf_theme_right_separator_sub/#tmux_conf_theme_right_separator_sub/" \
-e "s/^#tmux_conf_theme_left_separator_main/tmux_conf_theme_left_separator_main/" \
-e "s/^#tmux_conf_theme_left_separator_sub/tmux_conf_theme_left_separator_sub/" \
-e "s/^#tmux_conf_theme_right_separator_main/tmux_conf_theme_right_separator_main/" \
-e "s/^#tmux_conf_theme_right_separator_sub/tmux_conf_theme_right_separator_sub/" \
-i ~/.tmux.conf.local
禁用 C-a 前缀键:
sed -e "s/^# set -gu prefix2/set -gu prefix2/" \
-e "s/^# unbind C-a/unbind C-a/" \
-i ~/.tmux.conf.local
重载配置文件:
tmux source ~/.tmux.conf
如果需要自定义配置,请编辑 ~/.tmux.conf.local

使用
Ctrl-a作为第二组合键前缀(Ctrl-b前缀依然保留)+:最大化窗格m:切换鼠标模式-:上下分割窗格_:左右分割窗格
TPM
Oh my tmux! 预装了 TPM(Tmux Plugin Manager),可以用它来管理 tmux 插件。
I:安装插件u:更新插件Alt + u:删除未启用的插件
Tmux Resurrect
Tmux Resurrect 插件用来保存和恢复 tmux 窗格布局。
启用 Tmux Resurrect:
sed -i "s|^#set -g @plugin 'tmux-plugins/tmux-resurrect'|set -g @plugin 'tmux-plugins/tmux-resurrect'|" ~/.tmux.conf.local
prefix + Ctrl-s:保存prefix + Ctrl-r:恢复
参考:
主题
dracula.omt
-
安装主题:
git clone https://github.com/atgmello/dracula.omt.git ~/.config/oh-my-tmux/themes/dracula.omt ln -s ~/.config/oh-my-tmux/themes/dracula.omt/dracula.conf ~/.config/oh-my-tmux/themes/dracula.conf -
启用主题:
vim ~/.tmux.conf.localthemes_folder='~/.config/oh-my-tmux/themes/' theme="dracula.conf" if "[ -d ${themes_folder} ]" "source ${themes_folder}${theme}"
Troubleshooting
Tmux 继承了不想要的环境变量
Tmux server 启动时会继承所有环境变量,并在后续的每一个新会话中都会设置这些环境变量。有时可以删除不需要的环境变量:
tmux setenv -gu MY_ENV

浙公网安备 33010602011771号