多窗口管理器Tmux - 从入门到精通

tmux 的基本概念

我们先来理解下 tmux 的几个元素。tmux 的主要元素分为三层

  • Session 一组窗口的集合,通常用来概括同一个任务。session 可以有自己的名字便于任务之间的切换。
  • Window 单个可见窗口。Windows 有自己的编号,也可以认为和 ITerm2 中的 Tab 类似。
  • Pane 窗格,被划分成小块的窗口,类似于 Vim 中 C-w +v 后的效果。

下面是三个元素在 tmux 中的具体展现
《多窗口管理器Tmux - 从入门到精通》

安装需求

如果要安装 tmux, 需要先安装 libevent 2.0 以上版本

否则会出现如下错误:

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

安装 tmux

下载 tmux

https://github.com/tmux/tmux/releases/download/2.0/tmux-2.0.tar.gz

下载 libevent

http://sourceforge.net/projects/levent/?source=typ_redirect

编译安装 libevent

./configure --prefix=/home/harriszh/app && make && make install

因为考虑到很多用户在服务器上没有 root 权限,所以这里安装到 home 目录下

编译安装 tmux

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 的环境配置

setenv LD_LIBRARY_PATH /home/harriszh/app/lib
set path=(/home/harriszh/app/bin:$path)

打开 tmux

在命令行输入 tmux, 看看能否打开。
我遇到了 tmux 配置文件的问题,我在~/.tmux.conf 里把不支持的命令注释掉了

定制化

可以直接使用 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 - 从入门到精通》

使用

console 命令

开启会话: 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 模式,这时可以滚动窗口来选择

copy mode

在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-modecopy-mode-vi. 试了一下手工输入bind -Tcopy-mode Space send -X begin-selection果然就行了。 再通过网上查到了进入vi mode(set-window-option -g mode-keys vi)的方法并打入到.tmux.conf后,一切问题迎刃而解。

安装 tmuxinator

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
}

tmuxinator 常用命令

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 要执行的命令, 如果什么也不执行,就写上-

bug fix

在 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最大的好处是可以保存状态,对于登录到服务器工作的人,可以节省大量时间,而且多窗口省去了开非常多窗口切换的时间。使用它可以极大提高工作效率。而且可定制化,相较于同类瓦片式窗口管理器,提供了更多的定制和快捷键,是同类软件中的佼佼者。

    原文作者:harriszh
    原文地址: https://segmentfault.com/a/1190000016283278
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞