zoukankan      html  css  js  c++  java
  • tmux的使用方法和个性化配置

    介绍

    tmux是一个优秀的终端复用软件,即使非正常掉线,也能保证当前的任务运行,这一点对于远程SSH访问特别有用,网络不好的情况下仍然能保证工作现场不丢失!此外,tmux完全使用键盘控制窗口,实现窗口的切换功能。

    简单地说,tmux对于我主要有两个功能(这应该也是tmux的主要功能):

    1. split窗口。可以在一个terminal下打开多个终端,也可以对当前屏幕进行各种split,即可以 同时打开多个显示范围更小的终端。
    2. 在 使用SSH的环境下,避免网络不稳定,导致工作现场的丢失。想象以下场景, 你在执行一条命令的过程中,由于网络不稳定,SSH连接断开了。这个时候,你就不知道之前 的那条命令是否执行成功。如果此时你打开了很多文件,进入了较深层次的目录,由于网络 不稳定,SSH连接断开。重新连接以后,你又不得不重新打开那些文件,进入那个深层次的 目录。如果使用了tmux,重新连接以后,就可以直接回到原来的工作环境,不但提高了工作 效率,还降低了风险,增加了安全性。

    我自己的tmux配置文件(~/.tmux.conf):

    # 修改配置文件之后,可以在任何终端(包括tmux的终端)执行 tmux source-file ~/.tmux.conf 来立即使其生效,不用关闭session
    # 但是要加载颜色等的更改,可能需要使用 tmux ls 来查看所有session,
    # 然后
    tmux kill-session -a 或 tmux kill-session 或使用 tmux kill-session -t targetSession (targetSession 是整数) 来杀掉相应的session #设置前缀为Ctrl
    + k set -g prefix C-k #解除Ctrl+b 与前缀的对应关系 unbind C-b # set -g default-terminal "screen-256color" set -g default-terminal "xterm-256color" # prefix l 是向右切换面板,Ctrl+l清屏。 #up bind-key k select-pane -U #down bind-key j select-pane -D #left bind-key h select-pane -L #right bind-key l select-pane -R # 鼠标启用之后,终端不能再用鼠标选择文件进行拷贝,因此禁用鼠标 # Make mouse useful in copy mode # setw -g mode-mouse on # Allow mouse to select which pane to use # set -g mouse-select-pane on # Allow mouse dragging to resize panes # set -g mouse-resize-pane on # Allow mouse to select windows # set -g mouse-select-window on # Scroll History set -g history-limit 90000 # Set ability to capture on start and restore on exit window data when running an application setw -g alternate-screen on # Lower escape timing from 500ms to 50ms for quicker response to scroll-buffer access. set -s escape-time 50 #可以设置为vi或emacs set-window-option -g mode-keys vi # bind -t vi-copy 'v' begin-selection bind-key -T copy-mode-vi v send-keys -X begin-selection # bind -t vi-copy 'y' copy-selection bind-key -T copy-mode-vi y send-keys -X copy-selection #开启窗口的UTF-8支持 # It is no longer necessary for setting utf8 # set-window-option -g utf8 on # 安装网址: https://github.com/tmux-plugins/tmux-resurrect run-shell ~/.tmux-resurrect/resurrect.tmux # set pane-active-border-bg "#ff0000" # quotes for rgb, no quotes for name # don't rename windows automatically set-option -g allow-rename off # Info on left (I don't have a session display for now) set -g status-left '' # loud or quiet? set-option -g visual-activity off set-option -g visual-bell off set-option -g visual-silence off set-window-option -g monitor-activity off set-option -g bell-action none # The modes { setw -g clock-mode-colour colour135 setw -g mode-attr bold setw -g mode-fg colour196 setw -g mode-bg colour238 # } # The panes { set -g pane-border-bg colour16 set -g pane-border-fg colour236 set -g pane-active-border-bg colour16 set -g pane-active-border-fg colour3 # } # The statusbar { set -g status-justify left set -g status-interval 2 set -g status-position bottom set -g status-bg colour234 set -g status-fg colour149 set -g status-attr dim set -g status-left '' # set -g status-right '#[fg=colour233,bg=colour19,bold] %d/%m #[fg=colour233,bg=colour8,bold] %H:%M:%S ' set -g status-right-length 50 set -g status-left-length 20 setw -g window-status-current-fg colour1 setw -g window-status-current-bg colour11 setw -g window-status-current-attr bold setw -g window-status-current-format ' #I#[fg=colour1]:#[fg=colour21]#W#[fg=colour21]#F ' setw -g window-status-fg colour243 setw -g window-status-bg colour234 setw -g window-status-attr none setw -g window-status-format ' #I#[fg=colour243]:#[fg=colour243]#W#[fg=colour243]#F ' setw -g window-status-bell-attr bold setw -g window-status-bell-fg colour255 setw -g window-status-bell-bg colour1 # } # The messages { set -g message-attr bold set -g message-fg colour11 set -g message-bg colour16 set -g message-command-fg blue set -g message-command-bg black # }

    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 顺时针旋转当前窗口的面板

     临时将当前pannel全屏: CTRL+B Z,   再按一次恢复原来大小。

    临时执行命令:   prefix + :   ,这样可以临时执行配置命令。   如执行    set status-style "bg=black"

    tmux 如何手动 save session:                https://superuser.com/questions/440015/restore-tmux-session-after-reboot

    I wrote a simple bash script that persists open tmux sessions, windows and current working directories in each.

    Call it like so manually or periodically from cron (because you might forget):

    tmux-session save
    

    It will write to ~/.tmux-session. Restore them after reboot like so:

    tmux-session restore
    

    I find this much better than a several hundred line long Perl script.

    save session 帮助用到的网址:

    https://github.com/mislav/dotfiles/blob/d2af5900fce38238d1202aa43e7332b20add6205/bin/tmux-session

    tmux 在pannel中滚动(后来备注:强烈推荐使用复制模式来实现在pannel中滚动):                          https://superuser.com/questions/209437/how-do-i-scroll-in-tmux

    Ctrl-b then [ then you can use your normal navigation keys to scroll around (eg. Up Arrowor PgDn). Press q to quit scroll mode.

    Alternatively you can press Ctrl-b PgUp to go directly into copy mode and scroll one page up (which is what it sounds like you will want most of the time)

    In vi mode (see below), you can also scroll the page up/down line by line using Shift-k and Shift-j (if you're already in scroll mode). Unshifted, the cursor moves instead of the page.

    Excerpts from the man page:

    tmux may be controlled from an attached client by using a key combination of a prefix key, ‘C-b’ (Ctrl-b) by default, followed by a command key.

     The default command key bindings are:
    
    [           Enter copy mode to copy text or view the history.
    
    Function                     vi              emacs
    --------                     --              -----
    Half page down               C-d             M-Down
    Half page up                 C-u             M-Up
    Next page                    C-f             Page down
    Previous page                C-b             Page up
    Scroll down                  C-Down or C-e   C-Down
    Scroll up                    C-Up or C-y     C-Up
    Search again                 n               n
    Search again in reverse      N               N
    Search backward              ?               C-r
    Search forward               /               C-s
    

    Plus a bunch more. Note that you have to press C-b twice if you use that for page up since C-bis bound as the command key. See the man page for information on prefacing a copy mode command with a repeat count.

    You can set the key binding mode using Ctrl-b, then

    :set-window-option mode-keys vi
    

    or emacs.

    tmux的复制模式(含滚动):

    设置复制模式中的默认键盘布局;可以设置为vi或emacs,我是把这个设置写入了配置文件。
    set-window-option -g mode-keys vi          #可以设置为vi或emacs
    set-window-option -g utf8 on                    #开启窗口的UTF-8支持
    tmux复制模式的命令表:

           Function                        vi             emacs
           Back to indentation     ^              M-m
           Clear selection            Escape         C-g
           Copy selection            Enter          M-w
           Cursor down                j              Down
           Cursor left                     h              Left
           Cursor right                  l              Right
           Cursor to bottom line  L
           Cursor to middle line  M              M-r
           Cursor to top line         H              M-R
           Cursor up                      k              Up
           Delete entire line         d              C-u
           Delete to end of line   D              C-k
           End of line                    $              C-e
           Goto line                       :              g
           Half page down          C-d            M-Down
           Half page up               C-u            M-Up
           Next page                    C-f            Page down
           Next word                     w              M-f
           Paste buffer                  p              C-y
           Previous page             C-b            Page up
           Previous word              b              M-b
           Quit mode                     q              Escape
           Scroll down                   C-Down or J    C-Down
           Scroll up                        C-Up or K      C-Up
           Search again                n              n
           Search backward         ?              C-r
           Search forward             /               C-s
           Start of line                    0              C-a
           Start selection              Space          C-Space
           Transpose chars                           C-t

    复制模式步骤:
    我的控制键为:C-k
    1、 C-k [ 进入复制模式
    2、 参考上表移动鼠标到要复制的区域,移动鼠标时可用vim的搜索功能"/","?"
    3、 安空格键开始选择复制区域
    4、 选择完成后安enter键复制并退出
    5、 C-k ] 粘贴

    tmux session的保存和恢复:

    tmux-resurrect       https://github.com/tmux-plugins/tmux-resurrect             好用啊

    tmuxinator       https://github.com/tmuxinator/tmuxinator                     基于 ruby,好像只能手动配置session,以后启动自动加载这个session,但不能save工作中的session

    tmux 设置界面的颜色:

    首先必须参考 这篇文章 将ubuntu的fish shell terminal 设置为256色。

    https://github.com/seebi/dircolors-solarized                        里面有颜色列表

    https://github.com/seebi/tmux-colors-solarized

    https://github.com/erikw/tmux-powerline 

    可以使用如下命令在bash shell里打印各个颜色值:

    for i in {0..255}; do
        printf "x1b[38;5;${i}mcolour${i}x1b[0m
    "
    done
  • 相关阅读:
    两道挺有意思的思考题
    IEEE浮点数表示法
    MD5 Bump Mapping
    MD5 PolyBump + DetailBump
    DOOM3 MD5渲染方式的另一种猜测
    Relief Mapping
    如何计算投影纹理坐标
    Toon Shading, step 3
    c++数组初始化赋值
    c++重载部分运算符以及输入输出运算符
  • 原文地址:https://www.cnblogs.com/welhzh/p/5760593.html
Copyright © 2011-2022 走看看