2015-04-19 03:18:51 +00:00
|
|
|
|
---
|
|
|
|
|
category: tool
|
|
|
|
|
tool: tmux
|
2015-04-19 03:42:22 +00:00
|
|
|
|
filename: LearnTmux-cn.txt
|
2015-04-19 03:18:51 +00:00
|
|
|
|
contributors:
|
|
|
|
|
- ["mdln", "https://github.com/mdln"]
|
2015-04-19 03:42:22 +00:00
|
|
|
|
translators:
|
|
|
|
|
- ["Arnie97", "https://github.com/Arnie97"]
|
|
|
|
|
lang: zh-cn
|
2015-04-19 03:18:51 +00:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[tmux](http://tmux.sourceforge.net)
|
|
|
|
|
is a terminal multiplexer: it enables a number of terminals
|
|
|
|
|
to be created, accessed, and controlled from a single screen. tmux
|
|
|
|
|
may be detached from a screen and continue running in the background
|
|
|
|
|
then later reattached.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
tmux [command] # 运行一条命令
|
|
|
|
|
# 如果忽略 'tmux' 之后的命令,将会建立一个新的会话
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
new # 创建一个新的会话
|
|
|
|
|
-s "Session" # 创建一个named会话
|
|
|
|
|
-n "Window" # 创建一个named窗口
|
|
|
|
|
-c "/dir" # 在指定的工作目录中启动会话
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
attach # 连接到上一次的会话(如果可用)
|
|
|
|
|
-t "#" # 连接到指定的会话
|
|
|
|
|
-d # 断开其他客户端的会话
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
ls # 列出打开的会话
|
|
|
|
|
-a # 列出所有打开的会话
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
lsw # 列出窗口
|
|
|
|
|
-a # 列出所有窗口
|
|
|
|
|
-s # 列出会话中的所有窗口
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
lsp # 列出窗格
|
|
|
|
|
-a # 列出所有窗格
|
|
|
|
|
-s # 列出会话中的所有窗格
|
2015-04-19 03:18:51 +00:00
|
|
|
|
-t # List app panes in target
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
kill-window # 关闭当前窗口
|
|
|
|
|
-t "#" # 关闭指定的窗口
|
|
|
|
|
-a # 关闭所有窗口
|
|
|
|
|
-a -t "#" # 关闭除指定窗口以外的所有窗口
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
kill-session # 关闭当前会话
|
|
|
|
|
-t "#" # 关闭指定的会话
|
|
|
|
|
-a # 关闭所有会话
|
|
|
|
|
-a -t "#" # 关闭除指定会话以外的所有会话
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
## 快捷键
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 通过“前缀”快捷键,可以控制一个已经连入的tmux会话。
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
----------------------------------------------------------------------
|
2015-04-19 03:42:22 +00:00
|
|
|
|
(C-b) = Ctrl + b # 在使用下列快捷键之前,需要按这个“前缀”快捷键
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
(M-1) = Meta + 1 或 Alt + 1
|
2015-04-19 03:18:51 +00:00
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
? # 列出所有快捷键
|
|
|
|
|
: # 进入tmux的命令提示符
|
|
|
|
|
r # 强制重绘 the attached client
|
|
|
|
|
c # 创建一个新窗口
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
|
|
|
|
! # Break the current pane out of the window.
|
2015-04-19 03:42:22 +00:00
|
|
|
|
% # 将当前窗格分为左右两半
|
|
|
|
|
" # 将当前窗格分为上下两半
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
n # 切换到下一个窗口
|
|
|
|
|
p # 切换到上一个窗口
|
|
|
|
|
{ # 将当前窗格与上一个窗格交换
|
|
|
|
|
} # 将当前窗格与下一个窗格交换
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
s # 在交互式界面中,选择并连接至另一个会话
|
|
|
|
|
w # 在交互式界面中,选择并激活一个窗口
|
|
|
|
|
0 至 9 # 选择 0 到 9 号窗口
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
d # 断开当前客户端
|
|
|
|
|
D # 选择并断开一个客户端
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
& # 关闭当前窗口
|
|
|
|
|
x # 关闭当前窗格
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
Up, Down # 将焦点移动至相邻的窗格
|
2015-04-19 03:18:51 +00:00
|
|
|
|
Left, Right
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
M-1 到 M-5 # 排列窗格:
|
2015-04-19 03:18:51 +00:00
|
|
|
|
# 1) even-horizontal
|
|
|
|
|
# 2) even-vertical
|
|
|
|
|
# 3) main-horizontal
|
|
|
|
|
# 4) main-vertical
|
|
|
|
|
# 5) tiled
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
C-Up, C-Down # 改变当前窗格的大小,每按一次增减一个单位
|
2015-04-19 03:18:51 +00:00
|
|
|
|
C-Left, C-Right
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
M-Up, M-Down # 改变当前窗格的大小,每按一次增减五个单位
|
2015-04-19 03:18:51 +00:00
|
|
|
|
M-Left, M-Right
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
### 配置 ~/.tmux.conf
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
tmux.conf 可以在 tmux 启动时自动设置选项,类似于 .vimrc 或 init.el 的用法。
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
|
|
|
|
```
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# tmux.conf 示例
|
2015-04-19 03:18:51 +00:00
|
|
|
|
# 2014.10
|
|
|
|
|
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
### 通用设置
|
2015-04-19 03:18:51 +00:00
|
|
|
|
###########################################################################
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 启用 UTF-8 编码
|
2015-04-19 03:18:51 +00:00
|
|
|
|
setw -g utf8 on
|
|
|
|
|
set-option -g status-utf8 on
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 命令回滚/历史数量限制
|
2015-04-19 03:18:51 +00:00
|
|
|
|
set -g history-limit 2048
|
|
|
|
|
|
|
|
|
|
# Index Start
|
|
|
|
|
set -g base-index 1
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 启用鼠标
|
2015-04-19 03:18:51 +00:00
|
|
|
|
set-option -g mouse-select-pane on
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 重新加载配置文件
|
2015-04-19 03:18:51 +00:00
|
|
|
|
unbind r
|
|
|
|
|
bind r source-file ~/.tmux.conf
|
|
|
|
|
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
### 快捷键设置
|
2015-04-19 03:18:51 +00:00
|
|
|
|
###########################################################################
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 取消默认的前缀键 C-b
|
2015-04-19 03:18:51 +00:00
|
|
|
|
unbind C-b
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 设置新的前缀键
|
2015-04-19 03:18:51 +00:00
|
|
|
|
set-option -g prefix `
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 再次按下前缀键时,回到之前的窗口
|
2015-04-19 03:18:51 +00:00
|
|
|
|
bind C-a last-window
|
|
|
|
|
bind ` last-window
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 按下F11/F12,可以选择不同的前缀键
|
2015-04-19 03:18:51 +00:00
|
|
|
|
bind F11 set-option -g prefix C-a
|
|
|
|
|
bind F12 set-option -g prefix `
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# Vim 风格的快捷键绑定
|
2015-04-19 03:18:51 +00:00
|
|
|
|
setw -g mode-keys vi
|
|
|
|
|
set-option -g status-keys vi
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 使用 vim 风格的按键在窗格间移动
|
2015-04-19 03:18:51 +00:00
|
|
|
|
bind h select-pane -L
|
|
|
|
|
bind j select-pane -D
|
|
|
|
|
bind k select-pane -U
|
|
|
|
|
bind l select-pane -R
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 循环切换不同的窗口
|
2015-04-19 03:18:51 +00:00
|
|
|
|
bind e previous-window
|
|
|
|
|
bind f next-window
|
|
|
|
|
bind E swap-window -t -1
|
|
|
|
|
bind F swap-window -t +1
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 较易于使用的窗格分割快捷键
|
2015-04-19 03:18:51 +00:00
|
|
|
|
bind = split-window -h
|
|
|
|
|
bind - split-window -v
|
|
|
|
|
unbind '"'
|
|
|
|
|
unbind %
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 在嵌套使用 tmux 的情况下,激活最内层的会话,以便向其发送命令
|
2015-04-19 03:18:51 +00:00
|
|
|
|
bind a send-prefix
|
|
|
|
|
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
### 外观主题
|
2015-04-19 03:18:51 +00:00
|
|
|
|
###########################################################################
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 状态栏颜色
|
2015-04-19 03:18:51 +00:00
|
|
|
|
set-option -g status-justify left
|
|
|
|
|
set-option -g status-bg black
|
|
|
|
|
set-option -g status-fg white
|
|
|
|
|
set-option -g status-left-length 40
|
|
|
|
|
set-option -g status-right-length 80
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 窗格边框颜色
|
|
|
|
|
set-option -g 窗格-active-border-fg green
|
|
|
|
|
set-option -g 窗格-active-border-bg black
|
|
|
|
|
set-option -g 窗格-border-fg white
|
|
|
|
|
set-option -g 窗格-border-bg black
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 消息框颜色
|
2015-04-19 03:18:51 +00:00
|
|
|
|
set-option -g message-fg black
|
|
|
|
|
set-option -g message-bg green
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 窗口状态栏颜色
|
2015-04-19 03:18:51 +00:00
|
|
|
|
setw -g window-status-bg black
|
|
|
|
|
setw -g window-status-current-fg green
|
|
|
|
|
setw -g window-status-bell-attr default
|
|
|
|
|
setw -g window-status-bell-fg red
|
|
|
|
|
setw -g window-status-content-attr default
|
|
|
|
|
setw -g window-status-content-fg yellow
|
|
|
|
|
setw -g window-status-activity-attr default
|
|
|
|
|
setw -g window-status-activity-fg yellow
|
|
|
|
|
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
### 用户界面
|
2015-04-19 03:18:51 +00:00
|
|
|
|
###########################################################################
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 通知方式
|
2015-04-19 03:18:51 +00:00
|
|
|
|
setw -g monitor-activity on
|
|
|
|
|
set -g visual-activity on
|
|
|
|
|
set-option -g bell-action any
|
|
|
|
|
set-option -g visual-bell off
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 自动设置窗口标题
|
2015-04-19 03:18:51 +00:00
|
|
|
|
set-option -g set-titles on
|
2015-04-19 03:42:22 +00:00
|
|
|
|
set-option -g set-titles-string '#H:#S.#I.#P #W #T' # 窗口编号,程序名称,是否活动
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 调整状态栏
|
2015-04-19 03:18:51 +00:00
|
|
|
|
set -g status-left "#[fg=red] #H#[fg=green]:#[fg=white]#S#[fg=green] |#[default]"
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
# 在状态栏中显示性能计数器
|
|
|
|
|
# 需要用到 https://github.com/thewtex/tmux-mem-cpu-load
|
2015-04-19 03:18:51 +00:00
|
|
|
|
set -g status-interval 4
|
|
|
|
|
set -g status-right "#[fg=green] | #[fg=white]#(tmux-mem-cpu-load)#[fg=green] | #[fg=cyan]%H:%M #[default]"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
### 参考资料
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
[Tmux 主页](http://tmux.sourceforge.net)
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
[Tmux 手册](http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man1/tmux.1?query=tmux)
|
2015-04-19 03:18:51 +00:00
|
|
|
|
|
|
|
|
|
[Gentoo Wiki](http://wiki.gentoo.org/wiki/Tmux)
|
|
|
|
|
|
|
|
|
|
[Archlinux Wiki](https://wiki.archlinux.org/index.php/Tmux)
|
|
|
|
|
|
2015-04-19 03:42:22 +00:00
|
|
|
|
[如何在 tmux 状态栏中显示 CPU / 内存占用百分比](https://stackoverflow.com/questions/11558907/is-there-a-better-way-to-display-cpu-usage-in-tmux)
|