终端多窗口管理神器
------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的简介。
下载网址: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:
说明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
准备工作就绪后开始tmux的安装:
tar -zxvf tmux-1.6.tar.gz -C /usr/local/src
cd /usr/local/src/tmux-1.6
./configure --prefix=/usr/local/tmux
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
依个人喜好进行键绑定,个人习惯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
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
鼠标支持
set -g mouse-select-pane
set -g mouse-select-window
set -g mode-mouse
set -g mouse-resize-pane
默认路径或者命令
default-command
default-path
default-shell
default-terminal
默认启动应用, 当tmux启动时,可以默认启动一些应用:
new -s job
neww ranger
neww vim
selectw -t 3
#####################################################
vi ~/.bashrc
function tmux {
tmux
}
bind '"\C-x":"tmux\C-m"'
sudo apt-get install tmux
######
Tmux用法
######
set -g prefix ^a
unbind ^b
bind a send-prefix
注意:tmux与screen很大的一个不同点在于,tmux采用的是server/client模式,只要server不挂点,client理论上可以无限扩展,同时,tmux的管理层级又可以分为,client,session,window,pane. 一个client中有多个session,一个session中可以有很多个window,一个window中又可以分出很多个pane,所以就这一点而言是screen所望尘莫及的,并且窗口可以横着分也可以纵着分,真的是相当方便且不可多得的神器。
#########
常用命令(默认)
#########
开启tmux后通过"C-b ?"这几个组合键将调出tmux的全局帮助菜单,以下是tmux-1.6的默认帮助菜单
#################################################################
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key -r
bind-key -r
bind-key -r
bind-key -r
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key
bind-key -r
bind-key -r
bind-key -r
bind-key -r M-Right resize-pane -R 5
bind-key -r
bind-key -r
bind-key -r
bind-key -r C-Right resize-pane -R
虽然上面的帮助菜单已经很简单明,很清晰了,无需过多的解释,但这里还是啰嗦几点说明,更多地需要各位支尝试。
#################################################################
tmux
tmux ls
tmux attach-session -t 数字
C-z
C-b :
C-b c
C-b n
C-b p
C-b l
C-b s
C-b w
C-b [0-9]
C-b '
C-b &
C-b ,
C-b .
C-b "
C-b %
C-b o
C-b C-o
C-b ;
C-b space
C-b !
C-b x
C-b &
C-b #
C-b -
C-b =
C-b (
C-b )
C-b L
C-b {
C-b }
C-b [
C-b ]
C-b D
C-b d
C-b f
C-b q
C-b r
C-b ~
C-b pageup/pagedown
C-b 上下键
C-b C-方向键
对于其它键值这里就不多加说明,有兴趣的朋友可以自行尝试
################################################################
#####
补充说明
#####
********
1.控制键
********
*************
2.获取快捷键帮助
*************
************
3.快捷键小补充
************
********
4.复制粘贴
********
只需要以下两步,即可大大提高你的工作效率噢!
1.按 C-b [ 进入复制模式,如果有设置 setw -g mode-keys vi 的话,可按 vi 的按键模式操作。移动至待复制的文本处,按一下空格,结合 vi 移动命令开始选择,选好后按回车确认。
2.按 C-b ] 粘贴已复制的内容。
********
5.常用参数
********
更多:
http://wiki.freebsdchina.org/software/t/tmux
http://www.dayid.org/os/notes/tm.html
###########
###########
###########
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
- Different command interfaces. One of the goals of tmux is that the shell
- tmux calls window names (what you see in the status line) "names", screen
- tmux has a multiple paste buffers. Not a major one but comes in handy quite a
- tmux supports automatically renaming windows to the running application
- tmux has a choice of vi or emacs key layouts. Again, not major, but I use
- 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
- tmux's status line syntax is more readable and easier to use. I think it'd be
- tmux has modern, easily extended code. Again hard to argue screen is better
- tmux depends on libevent. I don't see this as a disadvantage: libevent is
- screen allows the window to be bigger than the terminal and can pan around
- screen has builtin serial and telnet support; this is bloat and is unlikely
- screen has support for updating utmp. Nobody has really come up with a clean,
- Environment handling is different.
- tmux tends to be more demanding on the terminal so tends to show up terminal
- 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
- 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"
* 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
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):
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:
* 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:
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:
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
- Creating a custom terminfo file that includes colors#256 in ~/.terminfo and
* 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:
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:
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):
* 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:
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
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:
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:
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):
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:
A possible reason is that /dev/null is not a character device or is otherwise
inaccessible.
Check with:
If it is not a character device or has incorrect permissions, it can typically
be recreated with:
* vim displays reverse video instead of italics, while less displays italics
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:
And tell tmux to use it in ~/.tmux.conf:
If your terminal supports 256 colors, use:
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:
$Id: FAQ 2554 2011-07-09 09:46:15Z tcunha
#########################################################
#########################################################