tmux基本用法与配置
一、什么是tmux
terminal multiplexer(终端复用器),它可以启动一系列终端会话
应用场景
- 单个终端查看操作多个会话
=》如图,一个终端挂多个会话和窗口,可以同时进行:编译、下code、上code、修改代码等 - 终端窗口后台运行
普通终端窗口:前台进程运行(如编译代码) --》 关闭窗口 --》 进程终止
tmux终端窗口:前台进程运行(如编译代码) --》 关闭窗口 --》 重新连接终端 --》 tmux attach重新连接tmux终端窗口
=》tmux是C/S架构,实际运行指令(如编译代码) 是在server,关闭窗口只是关闭client。我们可以打开新的窗口重新连接server
=》因此在做一些长时间任务时,比如下载代码,编译代码等,用tmux窗口来运行,这样即使网络不稳定终端窗口断开,命令也会在后台继续运行而不会被终止
二、基本概念
=》tmux元素分为三层
Session
一组窗口的集合,通常描述一个任务。Session可以定义名字
在终端,跑一个tmux,就会新建一个Session,tmux attach #Session_name连接一个创建过的Session
=》通常我会给每个项目建一个Session,可以快速切换不同任务
Window
单个可见窗口
=》在每个项目建立多个窗口,每个窗口作用不同工作(下载、上传、编译、编码)
Pane
=》在单个窗口中切分窗格
三、基本操作
tmux的操作都是用快捷键命令实现的。而 Prefix 是命令的前置操作,命令都是以 Prefix+cmd 组成,默认Prefix是Ctrl+b,比如新建一个窗口:按Ctrl+b,松开在按c键
直接终端输入“tmux”命令就是新建一个Session
Session操作
操作 | 快捷键 |
---|---|
查看/切换 | Prefix s |
离开 | Prefix d |
重命名 | Prefix $ |
Window操作
操作 | 快捷键 |
---|---|
查看/切换 | Prefix w |
新建 | Prefix c |
关闭 | Prefix & |
窗口号切换 | Prefix 窗口号 |
Pane操作
操作 | 快捷键 |
---|---|
垂直拆分 | Prefix " |
水平拆分 | Prefix % |
关闭当前 | Prefix x |
切换 | Prefix 方向键 |
Meta键设置
tmux中用到Meta作为快捷键,和其他按键组合起来实现某些tmux的指令,比如
bind -n M-s choose-session
Meta + s,唤起选择session的窗口。这样代替Prefix s,更加快速
=》Mac的option按键对应Windows的alt按键,也就是Meta按键。设置:在Iterm2中[Preferences]->[Profiles]->[Keys],把Left Option从Normal改为Esc+
四、自定义配置
% vim ~/.tmux.conf
修改后,重新敲tmux新建session后生效
常规
### terminal color setting
set -g default-terminal "screen-256color"
### time(ms) to show the message bar
set -g display-time 3000
set -g escape-time 200
### disable window title auto-rename
setw -g automatic-rename off
### monitor window message
setw -g monitor-activity on
set -g visual-activity on
### disable mouse
set -g mouse off
### set history limit
set-option -g history-limit 5000
=》打开鼠标模式会影响右键复制粘贴
Prefix
unbind C-b
set -g prefix C-a
bind C-a send-prefix
=》解绑Ctrl+b,设置Ctrl+a作为prefix
Reload
bind r source-file ~/.tmux.conf\; display "reload!!"
=》绑定r命令作为重新加载conf,就不用退出再进入session。如上Prefix r实现
复制粘贴模式
### set copy-mode as vi mode
setw -g mode-keys vi
### enter
bind s copy-mode
### select(v)
bind -T copy-mode-vi v send -X begin-selection
bind -T copy-mode-vi y send -X copy-selection-and-cancel
//bind -t vi-copy 'v' begin-selection
//bind -t vi-copy 'y' copy-selection
### paste(p)
bind -n M-y paste-buffer
=》设置为vi布局,Prefix s进入copy-mode,光标移动至要复制位置,按空格键后上下左右选择内容,按y结束,然后按Meta+y粘贴
Session操作
### select
bind -n M-s choose-session
### detach
bind -n M-d detach-client
=》Meta+s代替Prefix s
=》Meta+d实现断开client。重新连接用tmux attach session_name
Window操作
### set window index start
set -g base-index 1
### create new window
bind -n M-c new-window
### switch window
bind -n M-w choose-window
bind -n M-0 select-window -t :0
bind -n M-1 select-window -t :1
bind -n M-2 select-window -t :2
bind -n M-3 select-window -t :3
bind -n M-4 select-window -t :4
bind -n M-5 select-window -t :5
bind -n M-6 select-window -t :6
bind -n M-7 select-window -t :7
bind -n M-8 select-window -t :8
bind -n M-9 select-window -t :9
=》Meta+c 新建窗口
=》Meta+w 选择窗口
=》Meta+num 快速切换窗口
Pane操作
### set pane index start
set -g pane-base-index 1
### split-window by h or v
bind | split-window -h
bind - split-window -v
### switch pane
bind -n M-p select-pane -t :.+
bind -n M-h select-pane -L
bind -n M-j select-pane -D
bind -n M-k select-pane -U
bind -n M-l select-pane -R
### resize pane
bind -r M-Up resizep -U 5
bind -r M-Down resizep -D 5
bind -r M-Left resizep -L 5
bind -r M-Right resizep -R 5
=》Prefix | 垂直拆分窗口
=》Prefix - 水平拆分窗口
=》 Meta+h/j/k/ 上下左右切换窗口
配色方案
### default statusbar colors
set-option -g status-style fg=yellow,bg=black
### default window title colors
set-window-option -g window-status-style fg=brightblue,bg=default
### active window title colors
set-window-option -g window-status-current-style fg=brightred,bg=default
### pane border
set-option -g pane-border-style fg=black
set-option -g pane-active-border-style fg=brightgreen
### message text
set-option -g message-style fg=brightred,bg=black
### pane number display
set-option -g display-panes-active-colour blue
set-option -g display-panes-colour brightred
### clock
set-window-option -g clock-mode-colour green
### bell
set-window-option -g window-status-bell-style fg=black,bg=red
### status-bar is centre
set-option -g status-justify centre
==》新版
### default statusbar colors
set-option -g status-bg black
set-option -g status-fg yellow
set-option -g status-attr default
### default window title colors
set-window-option -g window-status-fg brightblue
set-window-option -g window-status-bg default
### active window title colors
set-window-option -g window-status-current-fg brightred
set-window-option -g window-status-current-bg default
### pane border
set-option -g pane-border-fg black
set-option -g pane-active-border-fg brightgreen
### message text
set-option -g message-bg black
set-option -g message-fg brightred
### pane number display
set-option -g display-panes-active-colour blue
set-option -g display-panes-colour brightred
### clock
set-window-option -g clock-mode-colour green
=》旧版