美文网首页
TMUX - terminal multiplexer

TMUX - terminal multiplexer

作者: tankywoo | 来源:发表于2013-11-15 14:24 被阅读0次

前序: 布道 Tmux

三个术语:

  • session: 管理多个window的会话
  • window: 一个window就是整个屏幕
  • pane: 一个window可以被横向或纵向分割为多个pane, 也就是俗称的分屏

tmux有很多组合键, 类似screen, tmux的组合键前缀(prefix)默认是C-b, 如果习惯了screen的C-a, 可以修改prefix, 以下都用C-b表示前缀

快捷键

基本操作

  • C-b : 进入tmux的命令行模式
  • C-b ? 显示所有的bind-key
  • C-b [ 进入复制模式
  • C-b ] 进入粘贴模式

如果有设置 setw -g mode-keys vi 的话,可按 vi 的按键模式操作。移动至待复制的文本处,按一下空格,结合 vi 移动命令开始选择,选好后按回车确认.

session 操作

  • C-b d deattch当前的session
  • C-b C-z 挂起当前的session
  • tmux attach [-t sessionname] 恢复session
  • C-b $ 可以重命名当前的session
  • tmux ls 显示tmux的所有session

window 操作

  • C-b c 可以新建一个新的window
  • C-b & 关闭当前的window
  • C-b , 可以重命名当前的window
  • C-b p 切换到前一个window
  • C-b n 切换到后一个window
  • C-b l 切换到上一次的window
  • C-b number 切换到指定编号的window, 默认从0开始
  • C-b w 显示当前会话的window, 可以通过上下选择来切换
  • tmux neww -n tmux 新建一个window, 名称是tmux

pane 操作

  • C-b " 将当前window横向分割为两个pane
  • C-b % 将当前window纵向分割为两个pane
  • C-b 方向键 在当前window里移动到其他pane
  • C-b o 切换到下一个pane
  • C-b Alt+方向键 调整pane的大小
  • C-b q 显示pane的编号
  • C-b x 关闭当前的pane, 会有确认提示. 也可以直接C-d
  • C-b { 把当前的pane移到左边
  • C-b } 把当前的pane移到右边
  • C-b z 把当前pane最大化/恢复. 感谢 yanyaoer 和 陈兴明Mingo 两位同学, tmux 升级到 1.8 后有这个特性了.
  • C-b Space 切换到下一个布局(这个布局应该是系统默认的一些)

配置文件

以下配置更新会有延迟, 最新配置见我的 dotfiles

set -g default-terminal "screen-256color"   # use 256 colors
set -g display-time 5000                    # status line messages display
set -g status-utf8 on                       # enable utf-8
set -g history-limit 100000                 # scrollback buffer n lines
setw -g mode-keys vi                        # use vi mode

# start window indexing at one instead of zero
set -g base-index 1

# set the prefix key and some key bindings to match GNU Screen
set -g prefix C-a
unbind-key C-b
bind-key C-a send-prefix

# key bindings for horizontal and vertical panes
unbind %
bind | split-window -h
unbind '"'
bind - split-window -v

# enable window titles
#set -g set-titles on

# window title string (uses statusbar variables)
set -g set-titles-string '#T'

# status bar with load and time
set -g status-bg '#4e4e4e'
set -g status-fg '#bbbbbb'
set -g status-left-fg '#55ff55'
set -g status-left-bg '#555555'
set -g status-right-fg '#55ff55'
set -g status-right-bg '#555555'
set -g status-left-length 90
set -g status-right-length 90
set -g status-left '[#(whoami)]'
set -g status-right '[#(date +" %m-%d %H:%M ")]'
set -g status-justify "centre"
set -g window-status-format '#I #W'
set -g window-status-current-format ' #I #W '
setw -g window-status-current-bg '#B3D9D9'
setw -g window-status-current-fg '#DDDDFF'

# pane border colors
set -g pane-active-border-fg '#55ff55'
set -g pane-border-fg '#555555'

# bind to reload config
bind r source-file ~/.tmux.conf

# add window to session
new -s tankywoo -n tankywoo
neww -n ops-dev
selectw -t 1

# scripting tmux
bind T source-file ~/.tmux/tanky

脚本化tmux

** 脚本化是 Tmux 的一大亮点 **

脚本化可以让我们自己定义一些脚本, 来构造自己的tmux布局

比如我写了一个分割三个pane的小脚本放在 ~/.tmux/tanky 里:

select-pane -t 0
split-window -h -p 60
select-pane -t 1
split-window -v -p 25
send-keys -t 0 'ipython' C-m
# The C-m at the end is interpreted by Tmux as the enter key.
select-pane -t 1

google搜出来的讲解tmux脚本化的E文不少, 不过没几个解释了 C-m 是干嘛的, 查看绑定键也没找到
后来在An Introduction to Scripting Tmux Key Bindings上看到了解释.

The C-m at the end is interpreted by Tmux as the enter key.

在 ~/.tmux.conf 里绑定快捷键: bind T source-file ~/.tmux/tanky

这样, 就可以通过快捷键 C-b S-t 一键初始化一个如下图的布局.


Script TmuxScript Tmux

另外, 还可以直接写shell脚本, 然后运行, 比如:

#!/bin/bash
# Tanky Woo@2013-06-19 10:51:15
# About:

tmux start-server

if ! $(tmux has-session -t 'tankywoo'); then
        tmux new-session -d -s 'tankywoo' -n 'tankywoo' # -d *
        tmux select-window -t 'tankywoo'
        tmux split-window -h -p 60
        tmux select-pane -t 1
        tmux split-window -v -p 25
        tmux send-keys -t 0 'ipython' C-m
        # The C-m at the end is interpreted by Tmux as the enter key.

        tmux new-window -n 'ops-dev'

        tmux select-window -t 'tankywoo'
        tmux select-pane -t 1
fi

tmux attach-session -d -t 'tankywoo'

下面这几个链接不错

技巧

批量操作

当需要在多个机器执行相同操作时, 可以考虑用pdsh等内容分发的工具, 而tmux也有它的一种强悍的方式. 在一个windows里打开多个pane, 每个pane登录一台服务器, 设置windows的选项, 在其中一个pane上操作时, 其它pane都会复制相同的操作.

在tmux的命令行里, 使用选项set synchronize-panes on即可.

在不同大小的屏幕打开一个session

** TODO **

比如在一个较小的桌面打开一个session, 然后又在一个较大的桌面也打开这个session:

tmux attach -t session-name

则会发现在较大的桌面上, 也只会显示和小桌面同样大小的窗口, 其余部分被密密麻麻的小点扩充.

解决方法之一是:

tmux attach -d -t session-name

即先强制 detach 掉小桌面的session, 然后再在较大桌面打开session.

另外, 看到很多帖子说可以设置:

setw -g aggressive-resize on

但是我设置后还是没有成功.

参考:

扩展 - tmux powerline

其他资料

历史记录

  • 2013-06-25 : 完善整个tmux wiki
  • 2013-07-04 : 补充批量操作技巧
  • 2013-07-29 : 开头引进博客的tmux简介; 对脚本化的强调说明;
  • 2013-08-05 : 补充 C-b z 特性
  • 2013-09-06 : tmux resize的问题
  • 2013-10-16 : 更新tmux.conf配置文件的链接到dotfiles

固定连接 :http://wiki.wutianqi.com/software/tmux.html

相关文章

网友评论

      本文标题:TMUX - terminal multiplexer

      本文链接:https://www.haomeiwen.com/subject/mhvqtttx.html