终端多窗口管理神器------tmux

#########################################################
终端多窗口管理

------tmux byobu screen terminator谁与争锋之tmux篇
#########################################################

######
Tmux简介
######

Developer(s) Nicholas Marriott
Initial release 2009-09-20
Stable release 1.6 / January 23, 2012; 4 months ago (2012-01-23)
Written in C
Operating system Unix-like
Available in English
Type Command line interface
License BSD
Website http://tmux.sourceforge.net/

tmux is a software application that can be used to multiplex several virtual consoles, allowing a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session. It is useful for dealing with multiple programs from a command line interface, and for separating programs from the Unix shell that started the program.[1] It provides many of the same functions as GNU Screen, but is distributed under a BSD license.

以上是wiki百科上对tmux的简介。

    不错,tmux 可以称得上为终端多窗口管理神器。tmux是一个可以在终端里开启多窗口的工具,它由 OpenBSD 团队基于C语言开发并维护的,是 OpenBSD 的基础系统软件之一,基于 BSD 许可证发布,可以称之为 GNU screen 的 BSD 替代版。除了 OpenBSD 外,它也可运行于 Linux 、Freebsd、AIX、IRIX,Opensolaris 等 OS 之上。目前的最新版为1.6.

下载网址:http://tmux.sourceforge.net/
https://github.com/tmux/tmux/wiki
######
Tmux功能
######   



* 提供了强劲的、易于使用的命令行界面,配置容易,尤其是状态行。
* 可横向和纵向分割窗口。
* 窗格可以自由移动和调整大小,或直接利用四个预设布局之一。
* 支持 UTF-8 编码及 256 色终端。
* 可在多个缓冲区进行复制和粘贴。
* 可通过交互式菜单来选择窗口、会话及客户端。
* 支持跨窗口搜索,自动命名窗口名称。
* 支持自动及手动锁定窗口。
* vi 或 emacs 按键绑定模式以及更改默认按键前缀。
* 脚本化,通过脚本可以方便的控制 tmux 会话。
* 文档清晰、详尽。

######
Tmux安装
######
CentOS6为例进行源码安装

将下载后的源码解压到任意目录,但习惯上我们将其解压到/usr/local/src目录下,然后进入该目录,参考FAQ和NOTE这两安装配置相关文件我们可以获得很多信息,包括安装步骤,配置,新版功能,与screen的详细比较等。我们会看到这一段:

Since the 1.2 release that tmux depends on libevent. Download it from:

    http://www.monkey.org/~provos/libevent/


说明tmux从1.2版本以后就依赖于libevent,所以我们需要先安装libevent,下载网址 http://www.monkey.org/~provos/libevent/
安装过程中可能还会依赖curse相关的包,如有提示也请一并安装。
对于配好本地yum源后,可以直接运行以下命令来安装curse相关的软件包。(yum源的名称以个人实际设置的为准,我个人直接用的默认值,c6-media)

yum --disablerepo=\* --enablerepo=c6-media install *curse*

libevent安装也相当简单,解压后直接用以下命令就OK
     ./configure && make
     sudo make install

准备工作就绪后开始tmux的安装:
tar -zxvf tmux-1.6.tar.gz -C /usr/local/src
cd /usr/local/src/tmux-1.6
./configure --prefix=/usr/local/tmux  (这里我指定安装到/usr/local/tmux这个目录,以便日后卸载的方便,当然,简单点可以直接./configure,这样就可以不用设置PATH和MAN的路径了)
make clean
make
make install


第一次运行可能会提示缺少libevent-2.0.so.5这个库,但前面明明已经安装成功,为什么还会出现该提示,搜索相关文档得知tmux调用函数库时的搜索路径不对,建个软链接就OK了。
ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5

指定PATH路径
vi ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/tmux/bin

指定MAN路径
vi /etc/man.conf
MANPATH_MAP    /usr/local/tmux/bin    /usr/local/tmux/share/man

依个人喜好进行键绑定,个人习惯VI,这里作VI的键绑定
ls /usr/local/src/tmux-1.6/examples

bash_completion_tmux.sh
h-boetes.conf
n-marriott.conf
screen-keys.conf
tmux_backup.sh
tmux.vim
t-williams.conf
vim-keys.conf
显然,tmux已经为我们想了很多,在解压文件中tmux已经为我们准备了一些常用的软件键绑定,有bash,C,screen,vi等相关的键映射配置文件,真是太人性化,太感动了。根据NOTE我们可以,
mkdir -p ~/.vim/syntax
cp /usr/local/src/tmux-1.6/examples/tmux.vim  ~/.vim/syntx
cat /usr/local/src/tmux-1.6/examples/vim-keys.conf > ~/.tmux.conf

######
Tmux配置
######
可以根据个人喜好来进一步设置自己喜欢的风格,下面是~/.tmux.conf的一些常用选项并以vi为键绑定,这在命令行模式下也是可行的

#####################################################
支持256位色
set -g default-terminal "screen-256color"

分割窗格,根据自己喜好来设置
##########################
#unbind '"'
#bind - splitw -v #横向分割
#unbind %
#bind | splitw -h #纵向分割
##########################

#bind s split-window -v  #横向分割
bind v split-window -h   #纵向分割


选择分割的窗格(如:^b+h就将光标左移)
bind h select-pane -L # 选择左窗格
bind j select-pane -D # 选择下窗格
bind k select-pane -U # 选择上窗格
bind l select-pane -R # 选择右窗格

重新调整窗格的大小(如:^b+<就将pane的左边界向左移动10个字符)
bind < resize-pane -L 10
bind > resize-pane -R 10
bind _ resize-pane -D 10
bind + resize-pane -U 10

vi模式的键绑定
setw -g mode-keys vi

":"进入命令行
bind : command-prompt

执行命令,比如:看 Manpage、查 Perl 函数,开启info,在新的window中进行ssh连机,自动载入fish
bind m command-prompt "splitw -h 'exec man %%'"
bind @ command-prompt "splitw -h 'exec perldoc -f %%'"
bind / command-prompt "split-window 'exec info %%'"
bind S command-prompt "new-window -n %1 'ssh %1'"

set -g default-command "exec /bin/fish"

定制状态行
状态行左边默认就很好了,我对右边定制了一下,显示 uptime 和 loadavg:
set -g status-right "#[fg=green]#(uptime.pl)#[default] • #[fg=green]#(cut -d ' ' -f 1-3 /proc/loadavg)#[default]"

设置状态行的背景和前景色:具体设置请自行查阅man
常用颜色:black,white,(bright)red,(bright)green,(bright)yellow,(bright)blue,(bright)magenta,(bright)cyan,and so on

#总的状态栏
set -g status-fg brightcyan
set -g status-bg white

#左右的状态栏
set -g status-left-fg brightred
set -g status-left-bg yellow
set -g status-right-fg brightgreen
set -g status-right-bg brightmagenta

#window状态栏
set -g window-status-fg brightyellow

set -g window-status-bg brightcyan
set -g window-status-activity-fg brightred
set -g window-status-activity-bg brightgreen
set -g window-status-current-fg brightred
set -g window-status-current-bg brightgreen
set -g window-status-content-fg yellow
set -g window-status-content-bg red
set -g window-status-bell-fg red
set -g window-status-bell-bg green

#pane相关的状态栏
set -g pane-border-fg brightyellow

set -g pane-border-bg brightblue
set -g pane-active-border-fg brightgreen
set -g pane-active-border-bg red

set -g message-fg brightred
set -g message-bg green
set -g message-command-fg brightblue
set -g message-command-bg cyan

set -g mode-fg brightred
set -g mode-bg green

#时钟颜色
set -g clock-mode-colour brightred
set -g clock-mode-style 12

    以上选项只是作简要的列举,并没有作很细致的设置,因个人喜好不同,所以这里就只作介绍,毕竟我们tmux的可定制性可谓超强,只要稍微花点时间就可以打造出酷炫的个人风格噢!!!

鼠标支持
set -g mouse-select-pane     [on|off]         #是否允许鼠标挑选pane
set -g mouse-select-window     [on|off]         #是否允许鼠标挑选window
set -g mode-mouse        [on|off|copy-mode]     #是否用于copy-mode中
set -g mouse-resize-pane    [on|off]         #是否允许鼠标调整pane的长和宽

默认路径或者命令
default-command
default-path
default-shell
default-terminal

默认启动应用, 当tmux启动时,可以默认启动一些应用:
new -s job    #新建名为job的会话
neww ranger   #启动ranger
neww vim      #启动vim
selectw -t 3  #默认选择标号为 3 的窗口
#####################################################

    最后我们还可以作更人性化的设置,就是定义快捷键,相信用过ubuntu的朋友一定知道,crl+alt+T是默认开启一个terminal,那么我们可不可以也通过按某个键来直接开启tmux呢?答案是肯定的,简单点,可以这样:
vi ~/.bashrc     增加下面这几行

function tmux {
tmux
}
bind '"\C-x":"tmux\C-m"'

    大家也知道,unix-like系统的快捷键是相当多的,就ctl加单个字母键组成的快捷键26个字母差不多没剩几个了,所以我这里选用了ctl+x来激 活tmux,这样1.不干扰原来默认的键绑定2.取的是tmux的最后一个字母x,也便于记忆。这样,不管是GUI还是CLI都可以通过ctl+x来激活 tmux,真的是相当的方便啊。

    CentOS6下的tmux-1.6的安装配置到此就告一段落,好好享用开源给我们带来的无限惊喜吧!
    哈哈,Ubuntu 下安装也相当方便,若不想编译安装可以直接从官方源安装,但不一定是最新版本,有些功能没有最新的全。运行下面的命令即可完成安装.

sudo apt-get install tmux


######
Tmux用法
######

    从screen切换到tmux应该没什么难度,tmux 的按键设置与 screen 大都相同,只是其默认按键前缀为 Ctrl-b。若想延续在 screen 中的使用习惯,可以在 $HOME/.tmux.conf 中进行设置:
set -g prefix ^a
unbind ^b
bind a send-prefix
    但我个人觉得^a不是很方便,因为与linux原生的快捷键绑定相冲突,而且^a在命令行相当实用,它可以快速将光标移动到命令行的开头,而这样绑定后则 原生的^a失效,用起来很不习惯,^b虽然在bash环境下往前无损退一格,但较^a来说,我觉得…^a更实用,所以我选择保持默认的^b。而且按着c- b不放,在 bash环境里的功能依然有效:)

注意:tmux与screen很大的一个不同点在于,tmux采用的是server/client模式,只要server不挂点,client理论上可以无限扩展,同时,tmux的管理层级又可以分为,client,session,window,pane. 一个client中有多个session,一个session中可以有很多个window,一个window中又可以分出很多个pane,所以就这一点而言是screen所望尘莫及的,并且窗口可以横着分也可以纵着分,真的是相当方便且不可多得的神器。
    对于tmux和screen的相同点和不同点以及常见问题可以查阅下面的FAQ官方原文(己附加在本文结尾)

#########
常用命令(默认)
#########


开启tmux后通过"C-b ?"这几个组合键将调出tmux的全局帮助菜单,以下是tmux-1.6的默认帮助菜单
#################################################################
bind-key        C-b send-prefix
bind-key        C-o rotate-window
bind-key        C-z suspend-client
bind-key      Space next-layout
bind-key          ! break-pane
bind-key          " split-window
bind-key          # list-buffers
bind-key          $ command-prompt -I #S "rename-session '%%'"
bind-key          % split-window -h
bind-key          & confirm-before -p "kill-window #W? (y/n)" kill-window
bind-key          ' command-prompt -p index "select-window -t ':%%'"
bind-key          ( switch-client -p
bind-key          ) switch-client -n
bind-key          , command-prompt -I #W "rename-window '%%'"
bind-key          - delete-buffer
bind-key          . command-prompt "move-window -t '%%'"
bind-key          0 select-window -t :0
bind-key          1 select-window -t :1
bind-key          2 select-window -t :2
bind-key          3 select-window -t :3
bind-key          4 select-window -t :4
bind-key          5 select-window -t :5
bind-key          6 select-window -t :6
bind-key          7 select-window -t :7
bind-key          8 select-window -t :8
bind-key          9 select-window -t :9
bind-key          : command-prompt
bind-key          ; last-pane
bind-key          = choose-buffer
bind-key          ? list-keys
bind-key          D choose-client
bind-key          L switch-client -l
bind-key          [ copy-mode
bind-key          ] paste-buffer
bind-key          c new-window
bind-key          d detach-client
bind-key          f command-prompt "find-window '%%'"
bind-key          i display-message
bind-key          l last-window
bind-key          n next-window
bind-key          o select-pane -t :.+
bind-key          p previous-window
bind-key          q display-panes
bind-key          r refresh-client
bind-key          s choose-session
bind-key          t clock-mode
bind-key          w choose-window
bind-key          x confirm-before -p "kill-pane #P? (y/n)" kill-pane
bind-key          { swap-pane -U
bind-key          } swap-pane -D
bind-key          ~ show-messages
bind-key      PPage copy-mode -u
bind-key -r      Up select-pane -U
bind-key -r    Down select-pane -D
bind-key -r    Left select-pane -L
bind-key -r   Right select-pane -R
bind-key        M-1 select-layout even-horizontal
bind-key        M-2 select-layout even-vertical
bind-key        M-3 select-layout main-horizontal
bind-key        M-4 select-layout main-vertical
bind-key        M-5 select-layout tiled
bind-key        M-n next-window -a
bind-key        M-o rotate-window -D
bind-key        M-p previous-window -a
bind-key -r    M-Up resize-pane -U 5
bind-key -r  M-Down resize-pane -D 5
bind-key -r  M-Left resize-pane -L 5
bind-key -r M-Right resize-pane -R 5
bind-key -r    C-Up resize-pane -U
bind-key -r  C-Down resize-pane -D
bind-key -r  C-Left resize-pane -L
bind-key -r C-Right resize-pane -R

虽然上面的帮助菜单已经很简单明,很清晰了,无需过多的解释,但这里还是啰嗦几点说明,更多地需要各位支尝试。
#################################################################
tmux                 #开启tmux  
tmux ls             #显示已有tmux列表(C-b s) 
tmux attach-session -t 数字     #选择session ,也可以简写tmux attach ,甚至tmux a
C-z                #挂起到后台,有的版本可能需要(C-b C-z)才能实现挂起
C-b :                #切换到命令行模式,类似于vi
C-b c                 #创建一个新的窗口(window)  
C-b n                 #切换到下一个窗口  
C-b p                 #切换到上一个窗口  
C-b l                 #切换到last-window
C-b s                #选择打开的session,默认是上下键或C-n,C-p来选择,vi绑定则是j和k,
C-b w                 #选择打开的window  
C-b [0-9]             #直接跳到你按的数字所在的窗口  
C-b '                #在index提示符下输入要跳转的窗口,如果窗口数超过了默认的9个,这种方法最为快捷
C-b &                 #重命名当前的session名称
C-b ,                 #重命名当前的window名称
C-b .                 #移动当前的window到某个session
C-b "                 #横向分割出来一个窗口  
C-b %                 #纵向分割出来一个窗口  
C-b o                 #小窗口(pane)位置不动,光标在pane中跳转切换  
C-b C-o             #光标位置不动,pane进行位置调换
C-b ;                #切换到last-pane
C-b space            #改变pane的显示方式,目前默认提供3种显示方式:水平,垂直,混合
C-b !                #将处于同一个window中的多个pane从该window中分离(独立)出来
C-b x                #交互地寻问用户是否关闭当前pane
C-b &                #交互地寻问用户是否关闭当前window
C-b #                #列出当前所有的buffer清单
C-b -                #删除当前默认的buffer
C-b =                #选择默认的buffer
C-b (                #切换到上一个session
C-b )                #切换到下一个session
C-b L                #在再次使用的session中进行rotate
C-b {                #切换当前当前pane到上一个pane的位置
C-b }                #切换当前当前pane到下一个pane的位置
C-b [                #copy-mode,此模式下默认通过^n,^p或上下键来移动,vi绑定后则可使用hjkl
C-b ]                #粘贴默认的buffer
C-b D                #选择client,如:/dev/pts/0 /dev/tty1 /dev/tty2 等
C-b d                 #保存退出一个会话
C-b f                #搜索窗口,在find-window提示符下输入要搜索窗口的名称即可定位到该窗口
C-b q                #显示当前window中所有分割的窗格并编号,光标所在窗口默认为红色,其余为蓝色
C-b r                #刷新clien
C-b ~                #显示消息,有点类似于日志信息
C-b pageup/pagedown        #查看历史记录
C-b 上下键              #上一个及下一个分隔窗口
C-b C-方向键             #调整分隔窗口大小

对于其它键值这里就不多加说明,有兴趣的朋友可以自行尝试
################################################################

#####
补充说明
#####


********
1.控制键
********
      控制键就是tmux的主键.当你在tmux环境下按下这个键的时候,tmux就会把你后面输入的指令,解析成它内置的功能.tmux默认的控制键是 ctrl+b(同时按下ctrl和b,在后面将直接用小写的c-b代替).这相当于screen的ctrl+a.同时用过两个工具的人总会争论 ctrl+a和ctrl+b的合理性.估计争论的也就是emacs和vi党.ctrl+a代表跳到命令的开始,而ctrl+b代表往回移动一格.从这一点 讲,我觉得用c-b没啥不好的.因为感觉c-a用到的情况要比c-b用到的多得多并且按着c-b不放,在 bash环境里的功能依然有效:)

*************
2.获取快捷键帮助
*************
      当按c-b的时候,要确保已经进入"tmux环境".进入的方法很简单.直接在命令行输入:tmux,或者像上面的那样定义一个快捷键,以后不管家是在 终端还是terminal中都可以通过快捷键来方便快捷地进入tmux环境噢!在ubuntu的官方源中可以直接通过apt-get来安装,虽然不见得是 最新版本但免去了编译的麻烦,对于不那么求新的同志来说是绝对够用的啦:)现在按C-b ?

      当外在copy-mode中的时候,要查看上面或者下面的内容,直接pageup,pagedown,vi模式则采用vi的快捷键,如:h j k l G gg ^f ^b ^d ^u等,emacs模式则可用^n ^p ^f ^b ^d ^u ^a ^e等。:)退出copy-mode按q即可。

************
3.快捷键小补充
************
      c-b c 创建一个新的窗口
      在你正后悔当前窗口被一个指令占用的时候,这个快捷键会解燃眉之急哟.当前窗口在任务栏会显示*,上一个窗口则会显示-

      c-b t 钟表
      显示一个钟表
      其实简单的时候没啥大用处,就是一个屏保的作用,不过你可以通过设置配置文件,使从钟表退出来的时候要输入密码:)

      查看历史记录
      一些命令执行的时候,显示结果可能总会超过一屏,怎么向上翻页以看到前面的东西呢?对,就是  C-b pageup/pagedown,实际上也是进入copy-mode,和C-b [ 的效果是一样的。退出的话,当然还是用q了

********
4.复制粘贴
********
    主要有两种实用的模式,对,那就是vi和emacs模式,当然,在没有设置键绑定时,tmux默认是emacs模式,和我们一般的shell环境比较像,通过man tmux,我们可以查阅到以下对照表:
      viemacs的模式对照表
           Function                     vi                    emacs
           Start of line                 0 or ^             C-a
           Clear selection           Escape          C-g
          Copy selection          Enter              M-w
           Cursor down                                   Down
           End of line                                     C-e
           Cursor left                                     Left
           Next page                 C-f                   Page down
           Next word                                     M-f
           Previous page         C-u                  Page up
           Previous word                              M-b
           Quit mode                                    Escape
           Cursor right                                  Right
          Start selection          Space          C-Space
           Cursor up                                    Up
    你发现什么?我们之前上下翻页用的page up和page down,也可以用上下键.是不是说明了我们这前用的模式就是emacs呢?默认模式为emacs,C-Space开始选择要复制的文本,然后通过^a ^e ^n ^p 等快捷键进行复制区域的选择,确定时按下alt+w(也就是上面对照表中的M-w,M就代表alt)即可。但这其中有一个问题,因为我们如果用 ctrl+space选择要复制的文字的话,只能在纯tty的终端并且输入启动没有设置成C-Space才会成功,因为会被系统优先抢占,这两个键被定义 到了激活输入法.所以,如果你想通过键盘用复制的功能的话,可以将模式改成vi,然后通过vi里支持的键选择.空格是选择的起点,然后上下或者左右移动选 择到终点,回车,就把内容复制了.这样的一个好处就是你可以复制到不在当前屏幕里的东西!      

只需要以下两步,即可大大提高你的工作效率噢!
1.按 C-b [ 进入复制模式,如果有设置 setw -g mode-keys vi 的话,可按 vi 的按键模式操作。移动至待复制的文本处,按一下空格,结合 vi 移动命令开始选择,选好后按回车确认。
2.按 C-b ] 粘贴已复制的内容。

********
5.常用参数
********
      tmux ls         #列出已有会话(list-sessions)
      tmux a        #连接到这前断开的会话(attach-session) 默认会连接上图列出的id最小的会话,你也可以指定你想连接到哪个会话:tmux a -t 1来连接到第一个会话
      tmux new        #创建新的会话(new-window) 当然了,不加这个参数也是会创建新会话的:)
      tmux kill-session -t session-name  #kill掉指定的session,当然该session中的所有window也将不复存在

更多:
http://wiki.freebsdchina.org/software/t/tmux
http://www.dayid.org/os/notes/tm.html

###########
   结语
###########
    关于tmux的介绍就先到这里.tmux的确是一个不可多得的神器,一旦你学会了它你真的会爱上她,很有可能会上瘾噢!当然,这个利器是不是你手上的利刃 取决于你花多少心思来运用它.以上的一些技巧,你只会一些基本的指令,就可以大大提高工作效率了,还等什么?赶快行动起来吧:)





###########
FAQ
###########


##################################################################
##################################################################
tmux frequently asked questions

******************************************************************************
* PLEASE NOTE: most display problems are due to incorrect TERM! Before       *
* reporting problems make SURE that TERM settings are correct inside and     *
* outside tmux.                                                              *
                                                                          *
* Inside tmux TERM must be "screen" or similar (such as "screen-256color").  *
* Don't bother reporting problems where it isn't!                            *
                                                                          *
* Outside, it must match your terminal: particularly, use "rxvt" for rxvt    *
* and derivatives.                                                           *
******************************************************************************

* How is tmux different from GNU screen?

tmux and GNU screen have many similarities. Some of the main differences I am
aware of are (bearing in mind I haven't used screen for a few years now):

- tmux uses a client-server model. Each server has single Unix domain socket in
  /tmp and within one server there are multiple sessions which may be attached
  to multiple clients (terminals).

  This has advantages, notably: windows may be linked simultaneously to
  multiple sessions; windows may be moved freely between sessions; and a client
  may be switched between sessions easily (C-b D). There is one major
  disadvantage: if the server crashes, game over, all sessions die. In
  practice, however, tmux is quite stable and gets more so as people report any
  bugs they hit :-).

  This model is different from screen, where typically each new screen instance
  is independent. tmux supports the same behaviour by using multiple servers
  with the -L option but it is not typically recommended.

- Different command interfaces. One of the goals of tmux is that the shell
  should be easily usable as a scripting language - almost all tmux commands
  can be used from the shell and behave identically whether used from the
  shell, from a key binding or from the command prompt. Personally I also find
  tmux's command interface much more consistent and clearer, but this is
  subjective.

- tmux calls window names (what you see in the status line) "names", screen
  calls them "titles".

- tmux has a multiple paste buffers. Not a major one but comes in handy quite a
  lot.

- tmux supports automatically renaming windows to the running application
  without gross hacks using escape sequences. Its even on by default.

- tmux has a choice of vi or emacs key layouts. Again, not major, but I use
  emacs so if tmux did support only one key set it would be emacs and then all
  the vi users would get humpy. Key bindings may be completely reconfigured in
  any case.

- tmux has an option to limit the window size.

- tmux has search in windows (C-b f).

- The window split (pane) model is different. tmux has two objects, windows and
  panes; screen has just windows. This difference has several implications:

  * In screen you can have a window appear in several layouts, in tmux a pane
  can only be in one window (fixing this is a big todo item but quite
  invasive).

  * tmux layouts are immutable and do not get changed unless you modify them.

  * In tmux, all panes are closed when you kill a window.

  * tmux panes do not have individual names, titles and so on.

  I think tmux's model is much easier to manage and navigate within a window,
  but breaking panes off from and joining them to windows is more clumsy.

  tmux also has support for preset pane layouts.

- tmux's status line syntax is more readable and easier to use. I think it'd be
  hard for anyone to argue with this. tmux doesn't support running a command
  constantly and always using the last line of its output, commands must be run
  again each time.

- tmux has modern, easily extended code. Again hard to argue screen is better
  if you have looked at the code.

- tmux depends on libevent. I don't see this as a disadvantage: libevent is
  small and portable, and on modern systems with current package management
  systems dependencies are not an issue. libevent brings advantages in code
  simplicity and performance.

- screen allows the window to be bigger than the terminal and can pan around
  it. tmux limits the size to the largest attached client. This is a big todo
  item for tmux but it is not trivial.

- screen has builtin serial and telnet support; this is bloat and is unlikely
  to be added to tmux.

- screen has support for updating utmp. Nobody has really come up with a clean,
  portable way to do this without making tmux setuid or setgid yet.

- Environment handling is different.

- tmux tends to be more demanding on the terminal so tends to show up terminal
  and application bugs which screen does not.

- screen has wider platform support, for example IRIX, and for odd terminals.

* I found a bug! What do I do?

Please send bug reports by email to nicm@users.sourceforge.net or
tmux-users@lists.sourceforge.net. Please include as much of the following
information as possible:

- the version of tmux you are running;
- the operating system you are using and its version;
- the terminal emulator you are using and the TERM setting when tmux was
  started;
- a description of the problem;
- if the problem is repeatable, the steps to repeat the problem;
- for screen corruption issues, a screenshot and the output of "infocmp $TERM"
  from outside tmux are often very useful.

* Why doesn't tmux do $x?

Please send feature requests by email to nicm@users.sourceforge.net.

* Why do you use the screen terminal description inside tmux? It sucks.

It is already widely available. It is planned to change to something else such
as xterm-xfree86 at some point, if possible.

* I don't see any colour in my terminal! Help!

On some platforms, common terminal descriptions such as xterm do not include
colour. screen ignores this, tmux does not. If the terminal emulator in use
supports colour, use a value for TERM which correctly lists this, such as
xterm-color.

* tmux freezes my terminal when I attach to a session. I even have to kill -9
  the shell it was started from to recover!

Some consoles really really don't like attempts to set the window title. Tell
tmux not to do this by turning off the "set-titles" option (you can do this
in .tmux.conf):

     set -g set-titles off

If this doesn't fix it, send a bug report.

* Why is C-b the prefix key? How do I change it?

The default key is C-b because the prototype of tmux was originally developed
inside screen and C-b was chosen not to clash with the screen meta key. It
also has the advantage of not interfering with the use of C-a for start-of-line
in emacs and the shell (although it does interfere with previous-character).

Changing is simple: change the "prefix-key" option, and - if required - move
the binding of the "send-prefix" command from C-b (C-b C-b sends C-b by
default) to the new key. For example:

    set -g prefix C-a
    unbind C-b
    bind C-a send-prefix

* How do I use UTF-8?

When running tmux in a UTF-8 capable terminal, UTF-8 must be turned on in tmux;
as of release 0.9, tmux attempts to autodetect a UTF-8-capable terminal by
checking the LC_ALL, LC_CTYPE and LANG environment variables. list-clients may
be used to check if this is detected correctly; if not, the -u command-line
flag may be specified when creating or attaching a client to a tmux session:

    $ tmux -u new

Since the 1.0 release, tmux will turn on UTF-8 related options automatically
(ie status-utf8, and utf8) if the above conditions are met.

* How do I use a 256 colour terminal?

Provided the underlying terminal supports 256 colours, it is usually sufficient
to add the following to ~/.tmux.conf:

    set -g default-terminal "screen-256color"

Note that some platforms do not support "screen-256color" ("infocmp
screen-256color" will return an error) - in this case see the next entry in
this FAQ.

tmux attempts to detect a 256 colour terminal both by looking at the colors
terminfo entry and by looking for the string "256col" in the TERM environment
variable.

If both these methods fail, the -2 flag may be passed to tmux when attaching
to a session to indicate the terminal supports 256 colours.

* vim or $otherprogram doesn't display 256 colours. What's up?

Some programs attempt to detect the number of colours a terminal is capable of
by checking the colors terminfo or Co termcap entry. However, this is not
reliable, and in any case is missing from the "screen" terminal description
used inside tmux.

There are two options (aside from using "screen-256color") to allow programs to
recognise they are running on a 256-colour terminal inside tmux:

- Manually force the application to use 256 colours always or if TERM is set to
  screen. For vim, you can do this by overriding the t_Co option, see
  http://vim.wikia.com/wiki/256_colors_in_vim.
- Creating a custom terminfo file that includes colors#256 in ~/.terminfo and
  using it instead. These may be compiled with tic(1).

* How do I make Ctrl-PgUp and Ctrl-PgDn work in vim?

tmux supports passing through ctrl (and where supported by the client terminal,
alt and shift) modifiers to function keys using xterm(1)-style key sequences.
This may be enabled per window, or globally with the tmux command:

    setw -g xterm-keys on

Because the TERM variable inside tmux must be set to "screen", vim will not
automatically detect these keys are available; however, the appropriate key
sequences can be overridden in .vimrc using the following:

    if &term == "screen"
    set t_kN=^[[6;*~
    set t_kP=^[[5;*~
    endif

And similarly for any other keys for which modifiers are desired.

Please note that the "xterm-keys" setting may affect other programs, in the
same way as running them in a standard xterm; for example most shells do not
expect to receive xterm(1)-style key sequences so this setting may prevent keys
such as ctrl-left and ctrl-right working correctly. tmux also passes through
the ctrl (bit 5 set, for example ^[[5~ to ^[[5^) modifier in non-xterm(1) mode;
it may be possible to configure vim to accept these, an example of how to do so
would be welcome.

vim users may also want to set the "ttyfast" option inside tmux.

* Why doesn't elinks set the window title inside tmux?

There isn't a way to detect if a terminal supports setting the window title, so
elinks attempts to guess by looking at the environment. Rather than looking for
TERM=screen, it uses the STY variable to detect if it is running in screen;
tmux does not use this so the check fails. A workaround is to set STY before
running elinks.

The following shell function does this, and also clears the window title on
exit (elinks, for some strange reason, sets it to the value of TERM):

    elinks() {
            STY= `which elinks` $*
            echo -ne \\033]0\;\\007;
    }

* What is the proper way to escape characters with #(command)?

When using the #(command) construction to include the output from a command in
the status line, the command will be parsed twice. First, when it's read by the
configuration file or the command-prompt parser, and second when the status
line is being drawn and the command is passed to the shell. For example, to
echo the string "(test)" to the status line, either single or double quotes
could be used:

    set -g status-right "#(echo \\\\(test\\\\))"
    set -g status-right '#(echo \\\(test\\\))'

In both cases, the status-right option will be set to the string "#(echo
\\(test\\))" and the command executed will be "echo \(test\)".

* tmux uses too much CPU. What do I do?

Automatic window renaming may use a lot of CPU, particularly on slow computers:
if this is a problem, turn it off with "setw -g automatic-rename off". If this
doesn't fix it, please report the problem.

* I use PuTTY and my tmux window pane separators are all qqqqqqqqq's!

PuTTY is using a character set translation that doesn't support ACS line
drawing. With a Unicode font, try setting PuTTY to use a different translation
on the Window -> Translation configuration page. For example, change UTF-8 to
ISO-8859-1 or CP437. It may also be necessary to adjust the way PuTTY treats
line drawing characters in the lower part of the same configuration page.

* What is the best way to display the load average? Why no #L?

It isn't possible to get the load average portably in code and it is preferable
not to add portability goop. The following works on at least Linux, *BSD and OS
X:

uptime|awk '{split(substr($0, index($0, "load")), a, ":"); print a[2]}'

* How do I attach the same session to multiple clients but with a different
  current window, like screen -x?

One or more of the windows can be linked into multiple sessions manually with
link-window, or a grouped session with all the windows can be created with
new-session -t.

* Ctrl and arrow keys doesn't work in putty! What do I do?

putty inverts the sense of the cursor key mode on ctrl, which is a bit hard for
tmux to detect properly. To get ctrl keys right, change the terminfo settings
so kUP5 (Ctrl-Up etc) are the adjusted versions, and disable smkx/rmkx so tmux
doesn't change the mode. For example with this line in .tmux.conf (assuming you
have TERM set to xterm):

set -g terminal-overrides "xterm*:kLFT5=\eOD:kRIT5=\eOC:kUP5=\eOA:kDN5=\eOB:smkx@:rmkx@"

Note that this will only work in tmux 1.2 and above.

* How can I blank the tmux window?

GNU screen has a feature whereby it will blank the screen after a period of
inactivity. To do the same thing in tmux, use the lock-command setting, for
example (with GNU bash):

set -g lock-command 'tput civis && read -s -n1'

This will remove the cursor and tell the shell to quit once a key has been
pressed. For zsh, use "read -s -k1".

In addition, it's possible to have both blanking and locking (for instance via
lock(1) or vlock(1)) by using the following:

bind x set lock-command '/usr/bin/vlock' \; lock-client \; set lock-command 'tput civis && read -s -n1'

* How can I open a new window in the same directory as the current window?

One option is to just run "TMUX= tmux" in the window. However, this only works if no
command is running, so that you can input the command.

A workaround is to let tmux know about the current path through an environment
variable. To do so, use the following command:

    [ -n "$TMUX" ] && tmux setenv TMUXPWD_$(tmux display -p "#I") $PWD

Which sets TMUXPWD_i (where i is the number of the current window) to the path
of the current directory. This command can be added to PS1, for example:

    PS1='$([ -n "$TMUX" ] && tmux setenv TMUXPWD_$(tmux display -p "#I") $PWD)\h$ '

When a new window is created, the shell should be asked to change
directory. You can define a new binding (for example, if using GNU bash):

    bind-key C-c run-shell 'tmux neww "cd $(tmux display -p "\$TMUXPWD_#I"); exec bash"'

This solution will work even if a command is currently running in the terminal,
but it will not work from a window that has just been swapped with another
because TMUXPWD_i will not be updated after a swap. However, once a new prompt
is displayed, TMUXPWD_i is updated properly.

* tmux doesn't start with "daemon failed"

tmux shows something similar to this when started:

    fatal: server_start: daemon failed: No such file or directory
    fatal: main_dispatch: imsg_read failed

A possible reason is that /dev/null is not a character device or is otherwise
inaccessible.

Check with:

    file /dev/null
    ls -l /dev/null

If it is not a character device or has incorrect permissions, it can typically
be recreated with:

    cd /dev && rm null && ./MAKEDEV null

* vim displays reverse video instead of italics, while less displays italics
  (or just regular text) instead of reverse. What's wrong?

Screen's terminfo description lacks italics mode and has standout mode in its
place, but using the same escape sequence that urxvt uses for italics. This
means applications (like vim) looking for italics will not find it and might
turn to reverse in its place, while applications (like less) asking for
standout will end up with italics instead of reverse. To make applications
aware that tmux supports italics and to use a proper escape sequence for
standout, you'll need to create a new terminfo file with modified sgr, smso,
rmso, sitm and ritm entries:

    $ mkdir $HOME/.terminfo/
    $ screen_terminfo="screen"
    $ infocmp "$screen_terminfo" | sed \
      -e 's/^screen[^|]*|[^,]*,/screen-it|screen with italics support,/' \
      -e 's/%?%p1%t;3%/%?%p1%t;7%/' \
      -e 's/smso=[^,]*,/smso=\\E[7m,/' \
      -e 's/rmso=[^,]*,/rmso=\\E[27m,/' \
      -e '$s/$/ sitm=\\E[3m, ritm=\\E[23m,/' > /tmp/screen.terminfo
    $ tic /tmp/screen.terminfo

And tell tmux to use it in ~/.tmux.conf:

    set -g default-terminal "screen-it"

If your terminal supports 256 colors, use:

    $ screen_terminfo="screen-256color"

instead of "screen". See the FAQ entry about 256 colors support for more info.
Also note that tmux will still display reverse video on terminals that do not
support italics.

If your urxvt cannot display italics at all, make sure you have an italics
capable font enabled, for example, add to ~/.Xdefaults:

    urxvt.italicFont: xft:Bitstream Vera Sans Mono:italic:autohint=true

$Id: FAQ 2554 2011-07-09 09:46:15Z tcunha
#########################################################
#########################################################
posted @ 2013-05-07 21:16  李庆喜  阅读(733)  评论(0编辑  收藏  举报