tmux是一个优秀的终端复用软件,即便非正常掉线,也能保证当前的任务运行,这一点对于远程SSH访问特别有用,网络很差的状况下仍然能保证工做现场不丢失!此外,tmux彻底使用键盘控制窗口,实现窗口的切换功能。html
简单地说,tmux对于我主要有两个功能(这应该也是tmux的主要功能):linux
我本身的tmux配置文件(~/.tmux.conf):git
# 修改配置文件以后,能够在任何终端(包括tmux的终端)执行 tmux source-file ~/.tmux.conf 来当即使其生效,不用关闭session
# 可是要加载颜色等的更改,可能须要使用 tmux 或 tmux kill-session 或使用 () 来杀掉相应的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 ls 来查看全部session,
# 而后kill-session -atmux kill-session -t targetSessiontargetSession 是整数
tmux快捷键:github
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, 再按一次恢复原来大小。
shell
临时执行命令: prefix + : ,这样能够临时执行配置命令。 如执行 set status-style "bg=black"ubuntu
tmux 如何手动 save session: https://superuser.com/questions/440015/restore-tmux-session-after-rebootvim
I wrote a simple bash script that persists open tmux sessions, windows and current working directories in each.windows
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-b
is 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
一、 C-k [ 进入复制模式
二、 参考上表移动鼠标到要复制的区域,移动鼠标时可用vim的搜索功能"/","?"
三、 安空格键开始选择复制区域
四、 选择完成后安enter键复制并退出
五、 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\n" done