「tmux」- terminal multiplexer

认识

官网:https://github.com/tmux/tmux/wiki
文档:http://man.openbsd.org/OpenBSD-current/man1/tmux.1

tmux,终端复用器,能够在单个终端里与多个 SHELL 进行交互;

tmux 是什么

tmux 可以让你在一个屏幕里操作多个终端(至少不用 ALT+TAB 来频繁的切换窗口)。也可以断开 tmux 与当前屏幕的绑定,断开之后依旧可以恢复连接状态(恢复到之前的状态);

当 tmux 启动时,会创建一个 session,该 session 连接了一个 window。屏幕底部的“绿行”是状态栏,显示了当前的 session 的信息,并可以用于输入交互命令;

session 是由 tmux 管理的伪终端集合。每个 session 可以连接多个 window。一个 window 占据了整个屏幕,并可以被分为多个 pane,每个 pane 都是一个独立的伪终端(man 4 pty);

任意数量的 tmux 实例可以连接到同一个 session 上,并且任意数量的 window 可能存在于同一个 session 中。如果结束了所有的 session,tmux 就退出了;

在 tmux 中,的 session 已经持久化了,如果意外断开了与服务器的连接(比如 ssh 超时)或者故意断开连接(使用‘C-b d’人为断开),可以使用tmux attach命令恢复之前的连接;

在 tmux 中,每个 client 在屏幕上显示一个 session,多个 session 由 tmux server 管理。tmux server 与 tmux client 进程分离,通过 /tmp 下的 socket 进行通信;

tmux 涉及的术语有:session、windows、pane;

组成

配置文件

~/.tmux.conf:默认的 tmux 配置文件。
/etc/tmux.conf:系统级的配置文件。系统范围(全局)的配置文件

tmux 启动时会执行$HOME/.profile 文件;

相关的程序、插件、配置

https://github.com/search?q=tmux&type=Repositories&ref=simplesearch

应用

常用设置及快捷键

Restore tmux session after reboot

https://superuser.com/questions/440015/restore-tmux-session-after-reboot

强制断开其他用户

How do I disconnect all other users in tmux?

Ctrl + B -> Shift + D
Ctrl + B -> : -> detach

调整 Pane 顺序

keyboard shortcuts – How does one swap two panes in Tmux? – Super User

向左移动:Ctrl + B => {
向右移动:Ctrl + B => }

注意事项,这里的“向左”、“向右”是指数字上的移动,而不是在界面中 Pane 位置的上线左右。

改进

常见错误整理

can’t create socket: No such file or directory

解决 tmux 启动「can’t create socket」的有关问题

执行 strace -e trace=file tmux 来查看错误原因。

no current target

How to get tmux to start with a couple of split windows

由于配置文件的解析是在会话创建之前,所以某些命令是无法执行的。比如 new-window 命令。

在启动启动时,如果希望 tmux 通过执行某些内置的指令,形成已经配置完成的窗口,这只能通过脚本去实现。

Bash Completion(自动补全)

wget -P ~/.local/share/bash-completion/completions/ \
    https://raw.githubusercontent.com/imomaliev/tmux-bash-completion/master/completions/tmux

chmod u+x ~/.local/share/bash-completion/completions/tmux

参考

undefined macro: AC_SEARCH_LIBS
Disable the public key check for rpm installation
https://leanpub.com/the-tao-of-tmux/read
Wikipedia / tmux: https://en.wikipedia.org/wiki/Tmux

相关书籍

《tmux 2:Productive Mouse-Free Development》