tmux神器

参考: http://wdxtub.com/2016/03/30/tmux-guide/

卢钧轶(cenalulu) http://cenalulu.github.io/linux/tmux/

tmux是“终端窗口管理器”,自己建立的窗口可以隐藏在后台,想看的时候再用一个命令调出来,真的很神器啊。

这样的好处就是可以离线操作服务器啦,在上面跑上任务,然后不用管,需要调出来看的时候再用tmux

使用终端窗口管理器就不会一下开五六个终端啦,桌面看起来也清爽了很多

同时

  • 窗口切换,每个窗口里还可以分割面板
  • 配置方便,可以使用脚本  ~/.tmux.conf   可以自定义很多快捷键
  • 类似vim的双层操作逻辑   ctrl+b就是命令模式  然后按单个字母就可以代替很长的命令
  • 复制黏贴缓冲区

安装方式:

sudo apt-get install tmux 

在终端输入tmux就可以打开一个新的tmux session

tmux的主要元素分为三层:

  • Session: 一组窗口的集合,通常用来概括同一个任务。
  • Window: 单个可见窗口
  • Pane: 每个窗口的分块

基本操作(常用命令)

我总结了些超级简便的命令,大部分都是用命令的模式,因为我实在不愿理敲这么长的命令啊

tmux所有的操作必须要先使用一个前缀键 (ctrl+b) 进入命令模式,或者说进入控制台,我把它改成了ctrl+a 因为自己手指不够长,按b很吃力

首先根据自己的任务建立session: 就像一个文件夹一样,有很多个窗口,每个窗口就是以前我们使用的终端

窗口控制

  • session会话: 是一个特定的终端组合,输入tmux就可以打开一个新的session 
    • tmux new -s session_name
    • tmux attach -t session_name 重新开启session
    • tmux switch -t session_name 转换到session     ctrl+a s
    • tmux ls/list-sessions列出现有的所有session
    • tmux detach 离开当前开启的session                 ctrl+a  d
    • tmux kill-server 关闭所有session                  ctrl+a kill-server   
  • window窗口:  session中可以有不同的window (但是同时只能看到一个window)
    • tmux new-window创建一个新的window            ctrl+a c
    • tmux list-windows                                           
    • tmux select-window -t:0-9  根据索引转到该window   ctrl+a  [0-9]
    • tmux rename-windwo 重命名当前的window
    • ctrl+a & 关闭当前窗口   p 切换到上一个窗口  n 切换到下一个窗口  | 前后窗口间相互切换  w通过窗口列表切换窗口
    • ctrl+a , 重命名窗口  . 修改当前创库编号  f 在所有窗口中查找关键词
  • pane面板: window中可以有不同的pane(可以把window分成不同的部分)
    • tmux split-window 将window垂直划分为两个pane
    • tmux split-window -h 将window水平划分为两个pane
    • " 上下分屏(可以改成 - )     % 左右分屏 (可以改成 | )
    • x 关闭当前分屏    !将当前面板提出置于新窗口
    • ctrl+方向键 以1个单位格为单位调整当前面板大小     alt+方向键 以5个单位格
    • q 显示面板编号  o 选择当前窗口中下一个面板  方向键 移动光标选择对应面板
    • { 向前置换当前面板   }  向后置换当前面板
  • 在tmux环境下的一些快捷键
    • ? 列出所有快捷键,按q返回
    • [ 复制模式,光标移动到复制内容位置,空格键开始,方向键选择复制,回车确认 q/Esc退出     
    • ] 进入粘贴模式,粘贴之前复制的内容, q/Esc退出

配置

修改  ~/.tmux.conf即可

#-- base --#
set -g default-terminal "screen-256color"
set -g display-time 3000
set -g history-limit 10000
set -g base-index 1
set -g pane-base-index 1
set -s escape-time 0
set -g mouse on
#-- bindkeys --#
# split windows like vim.  - Note: vim's definition of a horizontal/vertical split is reversed from tmux's
unbind s
bind s split-window -v
bind S split-window -v -l 40
bind v split-window -h
bind V split-window -h -l 120
# navigate panes with hjkl
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# key bindings for horizontal and vertical panes
unbind %
bind | split-window -h      # 使用|竖屏,方便分屏
unbind '"'
bind - split-window -v      # 使用-横屏,方便分屏
# swap panes
bind ^u swapp -U
bind ^d swapp -D
bind q killp
bind ^e last
unbind r
bind r source-file ~/.tmux.conf ; display "Configuration Reloaded!"
#-- statusbar --#
set -g status-justify centre
set -g status-left "#[fg=red]s#S:w#I.p#P#[default]"
set -g status-right '[#(whoami)#(date +" %m-%d %H:%M ")]'
set -g status-left-attr bright
set -g status-left-length 120
set -g status-right-length 120
set -g status-utf8 on
set -g status-interval 1
set -g visual-activity on
setw -g monitor-activity on
setw -g automatic-rename off
# default statusbar colors
set -g status-bg colour235 #base02
set -g status-fg colour136 #yellow
set -g status-attr default
# default window title colors
setw -g window-status-fg colour244
setw -g window-status-bg default
#setw -g window-status-attr dim
# active window title colors
setw -g window-status-current-fg colour166 #orange
setw -g window-status-current-bg default
#setw -g window-status-current-attr bright
# window title string (uses statusbar variables)
set -g set-titles-string '#T'
set -g status-justify "centre"
set -g window-status-format '#I #W'
set -g window-status-current-format ' #I #W '
# pane border
set -g pane-active-border-fg '#55ff55'
set -g pane-border-fg '#555555'
# message text
set -g message-bg colour235 #base02
set -g message-fg colour166 #orange
# pane number display
set -g display-panes-active-colour colour33 #blue
set -g display-panes-colour colour166 #orange
# clock
setw -g clock-mode-colour colour64 #green
# 修改进入命令模式按键
# remap prefix to Control + a
# set -g prefix C-a
# unbind C-b
# bind C-a send-prefix
原文地址:https://www.cnblogs.com/lainey/p/8593110.html