• 终端复用神器tmux简明教程


    使用GNU screen了很多年,猛然发现了一个比screen更好用的终端复用神器 -- tmux。本文将简明扼要地介绍这一神器。

    01 - 为什么要使用tmux?

    和screen一样,tmux允许一个任务持续运行,因此可以根据需要断开连接(比如下班回家),而不中断正在运行的任务。

    02 - 基本用法

    02.1 安装tmux

    • Fedora/CentOS
    sudo dnf install tmux # Fedora
    sudo yum install tmux # CentOS
    • Ubuntu
    sudo apt install tmux

    02.2 启动与退出

    如果在终端上键入命令tmux,就会开启一个新的会话。如果退出这个会话,就可以回到正常的shell中。

    $ tmux

    或者

    $ tmux new -s <session-name>

    按下Ctrl+d或者显式输入exit命令,就可以退出tmux。

    02.3 前缀键

    和GNU screen一样,tmux支持很多快捷键,而所有的快捷键都要通过前缀键唤起。GNU screen默认的前缀键是Ctrl+a, 而tmux默认的前缀键是Ctrl+b,即先按下Ctrl+b,快捷键才会生效。

    03 - 基本概念

    一个tmux会话(session)支持多个窗口(window),  一个tmux窗口支持多个窗格(pane)

    Basically, tmux sessions have windows, and windows have panes. 

    • session/window/pane from TMUX(1)

    03.1 Session(会话)

    会话(session)的本质就是一个连接,对应一个socket文件。打个比方,你拿起手机给别人打电话,电话一拨通,你们之间就建立了一个会话,只要手机不挂断,会话就不停。

    huanli@DELL380:~$ rm -rf /tmp/tmux-* && tmux new -s foo
    [detached (from session foo)] huanli@DELL380:~$ file /tmp/tmux-1000/default /tmp/tmux-1000/default: socket

    03.2 Window(窗口)

    开启一个会话(session)后,默认就会产生一个窗口(window),也可以增加新的窗口,一个窗口占据了整个屏幕。

    在下图中,开启了三个窗口,活动窗口是"1: window1*"

    03.3 Pane(窗格)

    tmux的窗口(window)可以根据需要开启多个窗格(pane),目前screen并不支持窗格,所以tmux更强大。

    在下图中,窗口1中开启了三个窗格。

    04 - 最简操作流程

    1. 新建一个会话: tmux new -s foo
    2. 在tmux窗口运行所需的程序
    3. 按快捷键Ctrl+b d将会话分离,回到shell
    4. 再次使用时,重新连接到会话:tmux attach-session -t foo

    05 - 会话(session)管理

    • 新建会话:tmux new -s <session name>
    • 分离会话:tmux detach
    • 查看会话:tmux ls
    • 接入会话:tmux attach -t <session-name OR session-id>
    • 切换会话:tmux switch -t <session-name OR session-id>
    • 杀掉会话:tmux kill-session -t <session-name OR session-id>
    • 重命名会话:tmux rename-session -t <old-session-name> <new-session-name>

    06 - 窗口(window)管理

    • 新建窗口:tmux new-window OR tmux new-window -n <window-name>
    • 切换窗口:tmux select-window -t <window-id OR window-name>
    • 重命名窗口:tmux rename-window <new-window-name>

    07 - 窗格(pane)管理

    • 划分窗格:tmux split-window
    # 上下划分 :tmux split-window
    # 左右划分 :tmux split-window -h
    • 移动光标:tmux select-pane
    # 光标切换到上方窗格: tmux select-pane -U
    # 光标切换到下方窗格: tmux select-pane -D
    # 光标切换到左边窗格: tmux select-pane -L
    # 光标切换到右边窗格: tmux select-pane -R
    • 交换窗格:tmux swap-pane
    # 当前窗格上移: tmux swap-pane -U
    # 当前窗格下移: tmux swap-pane -D

    08 - 快捷键 (来源戳这里

    These all play off of the Ctrl-b shortcut.
    
    Basics
    
        ? : get help
    
    Session management
    
        s : list sessions
        $ : rename the current session
        d : detach from the current session
    
    Windows
    
        c : create a new window
        , : rename the current window
        w : list windows
        % : split horizontally
        " : split vertically
        n : change to the next window
        p : change to the previous window
        0 to 9 : select windows 0 through 9
    
    Panes
    
        % : create a horizontal pane
        " : create a vertical pane
        h : move to the left pane. *
        j : move to the pane below *
        l : move to the right pane *
        k : move to the pane above *
        q : show pane numbers
        o : toggle between panes
        } : swap with next pane
        { : swap with previous pane
        ! : break the pane out of the window
        x : kill the current pane

    09 - 定制$HOME/.tmux.conf

    由于本人长期使用screen,习惯了快捷键前缀Ctrl+a,那么,可以将tmux的快捷键前缀Ctrl+b设置为Ctrl+a以实现使用习惯的平滑迁移。例如:

    # Remap prefix from 'C-b' to 'C-a'
    unbind C-b
    set-option -g prefix C-a
    bind-key C-a send-prefix

    将上述几行保存到你的~/.tmux.conf中,就可以将快捷键习惯从screen平滑迁移到tmux上了。更多定制,请参见这里

    参考资料:

  • 相关阅读:
    Python关键点常识
    sublime text 3创建新文件插件-AdvanceNewFile
    sublime 插件安装packagecontrol
    消息队列软件产品大比拼
    http://www.cnbc.com/2016/07/12/tensions-in-south-china-sea-to-persist-even-after-court-ruling.html
    Error: opening registry key 'SoftwareJavaSoftJava Runtime Environment' Error: could not find java.dll
    xlslib安装, aclocal-1.13: command not found, 安装升级autoconf-2.65.tar.gz, automake-1.13.tar.gz两个文件
    http://ftp.gnu.org/gnu/ http://ftp.gnu.org/gnu/libc/
    http://mirror.centos.org/centos/7.2.1511/os/x86_64/Packages/, 开源软件清单list
    ftp://fr2.rpmfind.net/linux/centos/7.2.1511/os/x86_64, 开源软件清单list
  • 原文地址:https://www.cnblogs.com/idorax/p/12378216.html
Copyright © 2020-2023  润新知