由于最近需要经常 ssh 到远程环境,遂趁此折腾了一番 tmux 。毕竟
工欲善其事,必先利其器以下是我的配置文件地址,并在不断摸索与更新中 https://github.com/shfshanyue/tmux-config 。
先放张截图,一睹为快,这是在 ubuntu 下的截图,在 Mac 下会更漂亮些。

特点
就我理解, tmux 有一个两大好处,分屏与 attach 。
分屏
诚然, iterm2 也是可以做到分屏的,但是 iterm2 有一个缺点便是 iterm for Mac 。而且, iterm2 分屏也需要不断地 ssh <server> 。导致的后果就是有多个用户连接,使用 ps aux | grep sshd | grep -v grep 查看连接数。
attach
attach 可以起到保护现场的作用,不至于因 ssh timeout ,而丧失了工作环境。
前提关于 tmux 的配置,我遵循几个原则
只添加新的快捷键,而尽量不覆盖旧的快捷键。比如 C-b 为前缀键,但是为了方便,往往会设置成 C-s ,但是也需要旧的快捷键 C-b 保留。
保持语义的情况下尽量使用简写,如下
source-file -> source bind-key -> bind set-option -> set set-window-option -> set -w Color Theme喜欢 solarized 主题,但是背景色在终端下不大漂亮,选择了 tomorrow 中的一个背景色替代。配置如下
set -g message-style "bg=#00346e, fg=#ffffd7" # tomorrow night blue, base3 set -g status-style "bg=#00346e, fg=#ffffd7" # tomorrow night blue, base3 set -g status-left "#[bg=#0087ff] #S " # blue set -g status-left-length 400 set -g status-right "#{?client_prefix, ~ , } #[bg=#0087ff] #h #[bg=red] %Y-%m-%d %H:%M " set -g status-right-length 600 set -wg window-status-format " #I #W " set -wg window-status-current-format " #I #W " set -wg window-status-separator "" set -wg window-status-current-style "bg=red" # red set -wg window-status-last-style "fg=red" Tips attch && detach每次新建 session 的时候带上名字,方便下次 attach 。稍微提一个命令 detach ,默认快捷键 prefix d 。
tmux new -s shanyue tmux detach tmux attach -t shanyue 对编辑和重启配置添加快捷键 bind r source ~/.tmux.conf; display "tmux config sourced" bind e neww -n tmux-config "${EDITOR:-vim} ~/.tmux.conf 保持当前路径新开 pane 和 window 时,保持当前路径。为以前的命令添加参数 -c ,表明心鉴窗口的路径。
bind c new-window -c "#{pane_current_path}" bind % split-window -h -c "#{pane_current_path}" bind '"' split-window -c "#{pane_current_path}" 熟练使用最大化与恢复当前 pane命令为 resize -Z ,默认快捷键为 prefix z 。比如高宽不够,则按 prefix z 进入全屏。相当酷的一个功能。
Last but not leastman tmux。不看文档不足以熟练,不看源码不足以精通。所以,平常需要多看几眼文档,多看几个命令。
扩展gpakosz/.tmux (stars 1569)
tmux-resurrect 保护和恢复工作状态