tmux – Linux终端管理软件

  • A+
所属分类:Linux

tmux是一个优秀的终端复用软件,类似GNU Screen,但来自于OpenBSD,采用BSD授权。使用它最直观的好处就是,通过一个终端登录远程主机并运行tmux后,在其中可以开启多个控制台而无需再“浪费”多余的终端来连接这台远程主机;当然其功能远不止于此。与screen相比的优点:可以横向和纵向分割窗口,且窗格可以自由移动和调整大小。可在多个缓冲区进行复制和粘贴,支持跨窗口搜索;非正常断线后不需重新detach;…… 有人说——与tmux相比,screen简直弱爆了。

下载源码包:

wget --no-check-certificate https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
wget http://downloads.sourceforge.net/tmux/tmux-1.8.tar.gz

编译tmux-1.8时,可能出现如下错误:

control.c: In function ‘control_callback’:
control.c:63: warning: implicit declaration of function ‘evbuffer_readln’
control.c:63: error: ‘EVBUFFER_EOL_LF’ undeclared (first use in this function)
control.c:63: error: (Each undeclared identifier is reported only once
control.c:63: error: for each function it appears in.)
control.c:63: warning: assignment makes pointer from integer without a cast
make: *** [control.o] Error 1

原因是使用的libevent的版本太低造成的,tmux1.8要求libevent>= 2.0.10-stable

编译libevent

yum -y install ncurses-devel #依赖包
tar xzf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure
make && make install
cd ../
 
tar xzf tmux-1.8.tar.gz
cd tmux-1.8
CFLAGS="-I/usr/local/include" LDFLAGS="-L//usr/local/lib" ./configure
make && make install1
 
<strong>直接./configure不能通过编译,报如下错误:</strong>
1cmd-capture-pane.o: In function `cmd_capture_pane_pending':
cmd-capture-pane.c:(.text+0x356): undefined reference to `evbuffer_pullup'
cmd-capture-pane.c:(.text+0x367): undefined reference to `evbuffer_get_length'
cmd-load-buffer.o: In function `cmd_load_buffer_callback':
cmd-load-buffer.c:(.text+0x52): undefined reference to `evbuffer_get_length'
cmd-load-buffer.c:(.text+0x88): undefined reference to `evbuffer_pullup'
cmd-pipe-pane.o: In function `cmd_pipe_pane_exec':
cmd-pipe-pane.c:(.text+0x2bf): undefined reference to `evbuffer_get_length'
cmd-run-shell.o: In function `cmd_run_shell_callback':
cmd-run-shell.c:(.text+0x315): undefined reference to `evbuffer_get_length'
cmd-run-shell.c:(.text+0x3fe): undefined reference to `evbuffer_pullup'
cmd-set-option.o: In function `cmd_set_option_exec':
cmd-set-option.c:(.text+0xc90): undefined reference to `event_initialized'
control.o: In function `control_callback':
control.c:(.text+0x15f): undefined reference to `evbuffer_readln'
control-notify.o: In function `control_notify_input':
control-notify.c:(.text+0x3ef): undefined reference to `evbuffer_pullup'
control-notify.c:(.text+0x3fa): undefined reference to `evbuffer_get_length'
format.o: In function `format_window_pane_tabs':
format.c:(.text+0xe05): undefined reference to `evbuffer_get_length'
format.c:(.text+0xe3b): undefined reference to `evbuffer_pullup'
format.c:(.text+0xe45): undefined reference to `evbuffer_get_length'
input.o: In function `input_set_state':
input.c:(.text+0x1b97): undefined reference to `evbuffer_get_length'
input.o: In function `input_parse':
input.c:(.text+0x1bd8): undefined reference to `evbuffer_get_length'
input.c:(.text+0x1c35): undefined reference to `evbuffer_pullup'
input.c:(.text+0x1c43): undefined reference to `evbuffer_get_length'
job.o: In function `job_write_callback':
job.c:(.text+0x12): undefined reference to `bufferevent_get_output'
job.c:(.text+0x1a): undefined reference to `evbuffer_get_length'
names.o: In function `queue_window_name':
names.c:(.text+0x144): undefined reference to `event_initialized'
names.o: In function `window_name_callback':
names.c:(.text+0x237): undefined reference to `event_initialized'
server-client.o: In function `server_client_check_exit':
server-client.c:(.text+0xbca): undefined reference to `evbuffer_get_length'
server-client.c:(.text+0xbdc): undefined reference to `evbuffer_get_length'
server-client.c:(.text+0xbee): undefined reference to `evbuffer_get_length'
server-client.o: In function `server_client_lost':
server-client.c:(.text+0x1835): undefined reference to `event_initialized'
server-client.c:(.text+0x1859): undefined reference to `event_initialized'
server-client.c:(.text+0x1937): undefined reference to `event_initialized'
server-fn.o: In function `server_update_event':
server-fn.c:(.text+0x23c): undefined reference to `event_initialized'
server-fn.o: In function `server_set_identify':
server-fn.c:(.text+0x2e7): undefined reference to `event_initialized'
server-fn.o: In function `server_push_stdout':
server-fn.c:(.text+0xd9f): undefined reference to `evbuffer_get_length'
server-fn.c:(.text+0xdcc): undefined reference to `evbuffer_pullup'
server-fn.o: In function `server_push_stderr':
server-fn.c:(.text+0xe6b): undefined reference to `evbuffer_get_length'
server-fn.c:(.text+0xe98): undefined reference to `evbuffer_pullup'
server.o: In function `server_add_accept':
server.c:(.text+0x43c): undefined reference to `event_initialized'
status.o: In function `status_message_set':
status.c:(.text+0x2484): undefined reference to `event_initialized'
status.o: In function `status_job_callback':
status.c:(.text+0x25e8): undefined reference to `evbuffer_get_length'
status.c:(.text+0x2623): undefined reference to `evbuffer_pullup'
tty-keys.o: In function `tty_keys_next':
tty-keys.c:(.text+0x6b7): undefined reference to `evbuffer_pullup'
tty-keys.c:(.text+0x6cb): undefined reference to `evbuffer_get_length'
tty-keys.c:(.text+0x771): undefined reference to `event_initialized'
tty-keys.c:(.text+0x843): undefined reference to `event_initialized'
tty-keys.c:(.text+0x8f9): undefined reference to `event_initialized'
tty.o: In function `tty_close':
tty.c:(.text+0x326c): undefined reference to `event_initialized'
window.o: In function `window_pane_read_callback':
window.c:(.text+0x276b): undefined reference to `evbuffer_get_length'
window.c:(.text+0x2791): undefined reference to `evbuffer_get_length'
window.c:(.text+0x27e1): undefined reference to `evbuffer_pullup'
window.o: In function `window_pane_destroy':
window.c:(.text+0x2e5a): undefined reference to `event_initialized'
window.o: In function `window_destroy':
window.c:(.text+0x36c5): undefined reference to `event_initialized'
collect2: ld returned 1 exit status
make: *** [tmux] Error 1

运行tmux可能出现如下错误:
tmux: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
解决办法:建立相应的symbol link

if [ `getconf WORD_BIT` = '32' ] && [ `getconf LONG_BIT` = '64' ] ; then
    ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
else
    ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
fi

判断操作系统多少位://blog.linuxeye.com/303.html
tmux操作手册
类似各种平铺式窗口管理器,tmux使用键盘操作,常用快捷键包括:

Ctrl+b 激活控制台;此时以下按键生效
系统操作 ? 列出所有快捷键;按q返回
d 脱离当前会话;这样可以暂时返回Shell界面,输入tmux attach能够重新进入之前的会话
D 选择要脱离的会话;在同时开启了多个会话时使用
Ctrl+z 挂起当前会话
r 强制重绘未脱离的会话
s 选择并切换会话;在同时开启了多个会话时使用
: 进入命令行模式;此时可以输入支持的命令,例如kill-server可以关闭服务器
[ 进入复制模式;此时的操作与vi/emacs相同,按q/Esc退出
~ 列出提示信息缓存;其中包含了之前tmux返回的各种提示信息
窗口操作 c 创建新窗口
& 关闭当前窗口
数字键 切换至指定窗口
p 切换至上一窗口
n 切换至下一窗口
l 在前后两个窗口间互相切换
w 通过窗口列表切换窗口
, 重命名当前窗口;这样便于识别
. 修改当前窗口编号;相当于窗口重新排序
f 在所有窗口中查找指定文本
面板操作 将当前面板平分为上下两块
% 将当前面板平分为左右两块
x 关闭当前面板
! 将当前面板置于新窗口;即新建一个窗口,其中仅包含当前面板
Ctrl+方向键 以1个单元格为单位移动边缘以调整当前面板大小
Alt+方向键 以5个单元格为单位移动边缘以调整当前面板大小
Space 在预置的面板布局中循环切换;依次包括even-horizontal、even-vertical、main-horizontal、main-vertical、tiled
q 显示面板编号
o 在当前窗口中选择下一面板
方向键 移动光标以选择面板
{ 向前置换当前面板
} 向后置换当前面板
Alt+o 逆时针旋转当前窗口的面板
Ctrl+o 顺时针旋转当前窗口的面板

tmux配置
tmux的系统级配置文件为/etc/tmux.conf,用户级配置文件为~/.tmux.conf。配置文件实际上就是tmux的命令集合,也就是说每行配置均可在进入命令行模式后输入生效。
下面是一个~/.tmux.conf的示例,其中包括了一些常用的配置:

#此类配置可以在命令行模式中输入show-options -g查询
set-option -g base-index 1                        #窗口的初始序号;默认为0,这里设置为1
set-option -g display-time 5000                   #提示信息的持续时间;设置足够的时间以避免看不清提示,单位为毫秒
set-option -g repeat-time 1000                    #控制台激活后的持续时间;设置合适的时间以避免每次操作都要先激活控制台,单位为毫秒
set-option -g status-keys vi                      #操作状态栏时的默认键盘布局;可以设置为vi或emacs
set-option -g status-right "#(date +%H:%M' ')"    #状态栏右方的内容;这里的设置将得到类似23:59的显示
set-option -g status-right-length 10              #状态栏右方的内容长度;建议把更多的空间留给状态栏左方(用于列出当前窗口)
set-option -g status-utf8 on                      开启状态栏的UTF-8支持
 
#此类设置可以在命令行模式中输入show-window-options -g查询
set-window-option -g mode-keys vi    #复制模式中的默认键盘布局;可以设置为vi或emacs
set-window-option -g utf8 on         #开启窗口的UTF-8支持
 
#将激活控制台的快捷键由Ctrl+b修改为Ctrl+a
set-option -g prefix C-a
unbind-key C-b
bind-key C-a send-prefix
 
#添加自定义快捷键
bind-key z kill-session                     #按z结束当前会话;相当于进入命令行模式后输入kill-session
bind-key h select-layout even-horizontal    #按h将当前面板布局切换为even-horizontal;相当于进入命令行模式后输入select-layout even-horizontal
bind-key v select-layout even-vertical      #按v将当前面板布局切换为even-vertical;相当于进入命令行模式后输入select-layout even-vertical1

#常用

tmux                             #开启tmux
tmux ls                         #列出会话(tmux list-session )
tmux attach                   #Tmux重新连接到之前的窗口可使用
tmux attach -t session     #进入某个会话
tmux -r                         #连接上次断开的session
tmux kill-session             #关闭上次tmux打开窗口
tmux kill-server              #关闭所有tmux打开窗口

Fri May 10 16:57:18 CST 2013

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: