咱们先来理解下 tmux 的几个元素。tmux 的主要元素分为三层git
下面是三个元素在 tmux 中的具体展示github
若是要安装 tmux, 须要先安装 libevent 2.0 以上版本shell
不然会出现以下错误:vim
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
https://github.com/tmux/tmux/releases/download/2.0/tmux-2.0.tar.gzwindows
http://sourceforge.net/projects/levent/?source=typ_redirectruby
./configure --prefix=/home/harriszh/app && make && make install
由于考虑到不少用户在服务器上没有 root 权限,因此这里安装到 home 目录下bash
setenv CFLAGS "-L/home/harriszh/app/lib -I/home/harriszh/app/include" && ./configure --prefix=/home/harriszh/app && make && make install
若是没有前面的 setenv, 那么会遇到下面的错误服务器
alerts.o: In function `alerts_queue': alerts.c:(.text+0xa0): undefined reference to `event_initialized' cmd-capture-pane.o: In function `cmd_capture_pane_exec': cmd-capture-pane.c:(.text+0x549): undefined reference to `evbuffer_pullup' cmd-capture-pane.c:(.text+0x554): undefined reference to `evbuffer_get_length' cmd-load-buffer.o: In function `cmd_load_buffer_callback': cmd-load-buffer.c:(.text+0x2cd): undefined reference to `evbuffer_get_length' cmd-load-buffer.c:(.text+0x2f9): undefined reference to `evbuffer_pullup' cmd-pipe-pane.o: In function `cmd_pipe_pane_exec': cmd-pipe-pane.c:(.text+0x221): undefined reference to `evbuffer_get_length' cmd-run-shell.o: In function `cmd_run_shell_callback': cmd-run-shell.c:(.text+0x297): undefined reference to `evbuffer_get_length' cmd-run-shell.c:(.text+0x33a): undefined reference to `evbuffer_pullup' control-notify.o: In function `control_notify_input': control-notify.c:(.text+0x4f0): undefined reference to `evbuffer_pullup' control-notify.c:(.text+0x4fb): undefined reference to `evbuffer_get_length' control.o: In function `control_callback': control.c:(.text+0x26): undefined reference to `evbuffer_readln' format.o: In function `format_cb_pane_tabs': format.c:(.text+0x69c): undefined reference to `evbuffer_get_length' format.c:(.text+0x6c8): undefined reference to `evbuffer_get_length' format.c:(.text+0x6da): undefined reference to `evbuffer_pullup' format.o: In function `format_job_complete': format.c:(.text+0xb1c): undefined reference to `evbuffer_get_length' format.c:(.text+0xb52): undefined reference to `evbuffer_pullup' format.o: In function `format_create': format.c:(.text+0x3a0a): undefined reference to `event_initialized' ...
下面是 cshell 的环境配置session
setenv LD_LIBRARY_PATH /home/harriszh/app/lib set path=(/home/harriszh/app/bin:$path)
在命令行输入 tmux, 看看可否打开。
我遇到了 tmux 配置文件的问题,我在~/.tmux.conf 里把不支持的命令注释掉了app
能够直接使用 gpakosz 的 tmux, 安装方法以下
$ cd $ rm -rf .tmux $ git clone https://github.com/gpakosz/.tmux.git $ ln -s .tmux/.tmux.conf $ cp .tmux/.tmux.conf.local .
要获取效果,能够直接tmux source ~/.tmux.conf
若是要使用近似 powerline 效果,能够在~/.tmux.conf.local 里注释 129-132 行, 打开 133-136 行
开启会话: tmux new -s <session-name>
断开会话: tmux deattach
接入以前的会话: tmux a -t <session-name>
关闭会话: tmux kill-session -t <session-name>
关闭窗口: tmux kill-session -t <session-name>
关闭 tmux: tmux killall
建立一个新的 window: tmux new-window
列出窗口: tmux list-windows
0-9 根据索引转到该 window: tmux select-window -t
重命名当前 window: tmux rename-window
将 window 垂直划分为两个 pane: tmux split-window
将 window 水平划分为两个 pane: tmux split-window -h
在指定的方向交换 pane: tmux swap-pane -[UDLR]
在指定的方向选择下一个 pane: tmux select-pane -[UDLR]
查看全局设定: tmux show-options -g
查看窗口设定: tmux show-options -w
查看remote设定: tmux show-options -s
下面的命令须要先按 prefix 键
? 获取帮助信息
s 列出全部会话
$ 重命名当前的会话
d 断开当前的会话
D 选择要脱离的会话;在同时开启多个会话时使用
[ 复制模式,光标移动到复制内容位置,空格键开始,方向键选择复制,回车确认,q/Esc 退出
] 粘贴模式,粘贴以前复制的内容,按 q/Esc 退出
t 显示当前时间
c 建立一个新窗口
& 关闭当前窗口
l 先后窗口间互相切换
. 修改当前窗口编号,至关于从新排序
f 在全部窗口中查找关键词
, 重命名当前窗口
w 列出全部窗口
% 水平分割窗口
" 竖直分割窗口
n 选择下一个窗口
p 选择上一个窗口
0~9 选择 0~9 对应的窗口
% 建立一个水平窗格
" 建立一个竖直窗格
h 将光标移入左侧的窗格*
j 将光标移入下方的窗格*
l 将光标移入右侧的窗格*
k 将光标移入上方的窗格*
q 显示窗格的编号
o 在窗格间切换
} 与下一个窗格交换位置
{ 与上一个窗格交换位置
ctrl+方向键 以 1 个单元格为单位移动边缘以调整当前窗格大小
alt+方向键 以 5 个单元格为单位移动边缘以调整当前窗格大小
alt+o 逆时针旋转当前窗格
ctrl+o 顺时针旋转当前窗格
z 最大化当前所在窗格
Page up 向上滚动屏幕,q 退出
Page down 向下滚动屏幕,q 退出
! 在新窗口中显示当前窗格
x 关闭当前窗格> 要使用带“*”的快捷键须要提早配置,配置方法能够参考上文的“在窗格间移动光标”一节。——译者注
t 在当前窗格显示时间
[ 进入 copy-paste 模式,这时能够滚动窗口来选择
在gpakosz/.tmux.git里的配置里已经为copy mode重设了类vi快捷键
但在个人某个环境里它们一直不工做,我花了几个小时终于搞清楚了缘由
在源代码key-bindings.c
里,有下面chars
"bind -Tcopy-mode C-Space send -X begin-selection", "bind -Tcopy-mode C-a send -X start-of-line", "bind -Tcopy-mode C-c send -X cancel", "bind -Tcopy-mode C-e send -X end-of-line", "bind -Tcopy-mode C-f send -X cursor-right", "bind -Tcopy-mode C-b send -X cursor-left", "bind -Tcopy-mode C-g send -X clear-selection", "bind -Tcopy-mode C-k send -X copy-end-of-line", "bind -Tcopy-mode C-n send -X cursor-down", ... "bind -Tcopy-mode-vi Space send -X begin-selection", "bind -Tcopy-mode-vi '$' send -X end-of-line", "bind -Tcopy-mode-vi , send -X jump-reverse", "bind -Tcopy-mode-vi / command-prompt -p'(search down)' 'send -X search-forward \"%%%\"'", "bind -Tcopy-mode-vi 0 send -X start-of-line",
一开始我经过C-a :
来手工输入bind -Tcopy-mode-vi Space send -X begin-selection
,但发现仍是没用, 特别是后来我注意到要用C-Space
, 而不是Space
, 我才意识到有两种模式, copy-mode
和copy-mode-vi
. 试了一下手工输入bind -Tcopy-mode Space send -X begin-selection
果真就好了。 再经过网上查到了进入vi mode(set-window-option -g mode-keys vi
)的方法并打入到.tmux.conf后,一切问题迎刃而解。
tmuxinator 是 tmux 的配置管理工具, 解决了 tmux 服务器关机后 session 丢失问题。tmuxinator 能够根据配置文件快速建立 tmux 的 session。
gem install tmuxinator
在$HOME/.tmuinator/.tmuxinator.bash 里新建
#!/usr/bin/env bash _tmuxinator() { COMPREPLY=() local word word="${COMP_WORDS[COMP_CWORD]}" if [ "$COMP_CWORD" -eq 1 ]; then local commands="$(compgen -W "$(tmuxinator commands)" -- "$word")" local projects="$(compgen -W "$(tmuxinator completions start)" -- "$word")" COMPREPLY=( $commands $projects ) elif [ "$COMP_CWORD" -eq 2 ]; then local words words=("${COMP_WORDS[@]}") unset words[0] unset words[$COMP_CWORD] local completions completions=$(tmuxinator completions "${words[@]}") COMPREPLY=( $(compgen -W "$completions" -- "$word") ) fi } complete -F _tmuxinator tmuxinator mux
而后在$HOME/.bashrc 里增长
source $HOME/.tmuxinator/.tmuxinator.bash export EDITOR='vim'
source $HOME/.bashrc 使其生效
若是用的是 zhs, 使用下面文件
_tmuxinator() { local commands projects commands=(${(f)"$(tmuxinator commands zsh)"}) projects=(${(f)"$(tmuxinator completions start)"}) if (( CURRENT == 2 )); then _describe -t commands "tmuxinator subcommands" commands _describe -t projects "tmuxinator projects" projects elif (( CURRENT == 3)); then case $words[2] in copy|debug|delete|open|start) _arguments '*:projects:($projects)' ;; esac fi return }
mux n ws # 建立工程 ws
mux o ws # 打开工程 ws 的配置文件
mux e ws # 同上
mux c ws ws1 # 复制 ws 工程到 ws1
mux d ws # 删除 ws 工程
mux l # 显示全部工程
mux ws # 开启 ws 工程
在 new 一个工程后,会打开一个文本
name: ws # session名称 root: ~/ # 工程根目录,活动Pane会首先cd到此目录 windows: - editor: # 第1个名为Editor的Window layout: main-vertical # Pane的布局 panes: # 各个Pane - vim # 第一个Pane运行vim命令 - guard # 第二个Pane运行guard命令 - server: bundle exec rails s # 第2个名为server的Window,运行命令为bundle - logs: tail -f log/development.log # 第3个名为logs的Window,运行命令为tail
能够修改上面的 editor, server, logs (window 名)
或者 panes 下面的各个 panes 要执行的命令, 若是什么也不执行,就写上-
在 tmuxinator 里,删除工程时会报以下错误
$ tmuxinator d ws Are you sure you want to delete ws?(y/n) y /usr/local/lib/ruby/gems/2.4.0/gems/tmuxinator-0.9.0/lib/tmuxinator/cli.rb:220:in `block in delete': uninitialized constant Tmuxinator::Cli::FileUtils (NameError) Did you mean? FileTest from /usr/local/lib/ruby/gems/2.4.0/gems/tmuxinator-0.9.0/lib/tmuxinator/cli.rb:215:in `each' from /usr/local/lib/ruby/gems/2.4.0/gems/tmuxinator-0.9.0/lib/tmuxinator/cli.rb:215:in `delete' from /usr/local/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run' from /usr/local/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command' from /usr/local/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch' from /usr/local/lib/ruby/gems/2.4.0/gems/thor-0.20.0/lib/thor/base.rb:466:in `start' from /usr/local/lib/ruby/gems/2.4.0/gems/tmuxinator-0.9.0/bin/tmuxinator:15:in `<top (required)>' from /usr/local/bin/tmuxinator:23:in `load' from /usr/local/bin/tmuxinator:23:in `<main>'
须要在/usr/local/lib/ruby/gems/2.4.0/gems/tmuxinator-0.9.0/lib/tmuxinator/cli.rb 第 2 行加上
require 'fileutils'
tmux最大的好处是能够保存状态,对于登陆到服务器工做的人,能够节省大量时间,并且多窗口省去了开很是多窗口切换的时间。使用它能够极大提升工做效率。并且可定制化,相较于同类瓦片式窗口管理器,提供了更多的定制和快捷键,是同类软件中的佼佼者。