安裝前准備
- tmux-plugins 要求 tmux 版本1.9以上, 以及 git, bash。
- 如果使用的是 CentOS 系統,請確保安裝 tmux 版本大於1.8,通常直接
yum install的版本就是1.8,建議下載源碼進行編譯。https://github.com/tmux/tmux/releases/ - 如果使用的是 Ubuntu 系統,
apt install -y tmux下來的版本已符合要求,不需要源碼編譯。
安裝 tmux-plugins 和 tmux-logging
- 安裝 tpm (tmux-plugins manager)
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
- 執行
vim ~/.tmux.conf進行配置 (或者vim $XDG_CONFIG_HOME/tmux/tmux.conf皆可)
粘貼下方代碼
# Display color
set -g default-terminal "screen-256color"
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-logging'
set -g history-limit 10000000
run -b '~/.tmux/plugins/tpm/tpm'
- 啟動 tumx 並激活配置
$ tmux
#(會切換到 tmux shell)
$ tmux source ~/.tmux.conf
- 安裝插件(一鍵安裝)
- (如果已執行第2步,請忽略)向
~/.tmux.conf添加一行with set -g @plugin 'xxx',xxx為插件名稱- 按下
<prefix> + shift + i,tmux將自動安裝和配置插件到~/.tmux/plugins/目錄
- 卸載插件(非必須)
- 從
~/.tmux.conf刪除或注釋掉with set -g @plugin 'xxx'- 按下
<prefix> + alt + u,tmux將自動將插件移除
tmux-logging 的三種常用功能
- 日志記錄
按下
prefix + shift + p開啟/關閉日志記錄,即按下一次開啟,再按下一次關閉,保存期間的日志內容
log 文件格式:tmux-#{session_name}-#{window_index}-#{pane_index}-%Y%m%dT%H%M%S.log
log 文件保存目錄:$HOME (用戶 home 目錄)
例子:~/tmux-my-session-0-1-20140527T165614.log
- 日志截屏
按下
prefix + alt + p,保存當前區域的可見文本,以文本形式保存屏幕截圖
log 文件格式:tmux-screen-capture-#{session_name}-#{window_index}-#{pane_index}-%Y%m%dT%H%M%S.log
log 文件保存目錄:$HOME (用戶 home 目錄)
例子:tmux-screen-capture-my-session-0-1-20140527T165614.log
- 歷史記錄(最常用)
Save complete pane history to a file. Convenient if you retroactively remember you need to log/save all the work.
按下
prefix + alt + shift + p,保存當前區域自啟動以來的所有日志記錄,有緩沖區限制,在.tmux.conf中設置set -g history-limit 50000,可保存最多50000行記錄,最大數量視系統內存而定。
log 文件格式:tmux-history-#{session_name}-#{window_index}-#{pane_index}-%Y%m%dT%H%M%S.log
log 文件保存目錄:$HOME (user home dir)
例子:tmux-history-my-session-0-1-20140527T165614.log
tmux 備忘錄
轉自https://github.com/skywind3000/awesome-cheatsheets
##############################################################################
# session
##############################################################################
tmux # 創建新的 session
tmux new -S name # 創建新的 session 並指定一個name
tmux ls # 查看多少個后台工作的 session
tmux a/at/attach # 重新連接 session
tmux a/at/attach -t num # 如果有多個 session, 指定 session num 連接
tmux kill-ses -t myses # kill 某個 session
tmux kill-ses -a # kill 所有 session, 除了當前的 session
tmux kill-ses -a -t myses # kill 所有 session, 除了 myses
##############################################################################
# 操作方式
##############################################################################
<prefix> + key # <prefix> 默認為 ctrl + b
<prefix> + c # 表示先按 ctrl + b 再按 c 鍵
##############################################################################
# 幫助信息
##############################################################################
<prefix> + ? # 查看所有的 key map
##############################################################################
# window 操作
##############################################################################
<prefix> + c # 新建一個 window
<prefix> + n # 下一個 window
<prefix> + p # 上一個 window
<prefix> + w # 列出 window
<prefix> + & # 關閉當前 window
<prefix> + num[1-9] # 選定特定 num 的 window
<prefix> + f # 查找 window
<prefix> + , # 重命名 window
<prefix> + . # 移動 window
##############################################################################
# pane 操作
##############################################################################
<prefix> + % # 縱向分裂
<prefix> + " # 橫向分裂
<prefix> + 方向鍵 # 在一個 window 中切換 pane
<prefix> + ctrl-方向鍵 # 調整 pane 大小
<prefix> + z # 全屏化當前 pane, 再次執行退出全屏
<prefix> + x # 關閉當前 pane
<prefix> + q # 顯示 pane 編號
<prefix> + o # 跳到下一個 pane
<prefix> + { # 跟前一個編號的 pane 交換
<prefix> + } # 跟后一個編號的 pane 交換
<prefix> + ; # 跳轉到上一個活躍的 pane
<prefix> + ! # 將 pane 轉化為 window
<prefix> + <Space> # 改變 pane 的布局
##############################################################################
# session
##############################################################################
<prefix> + d # detach 整個session, 后續可以重新連接
<prefix> + s # 列出 session
<prefix> + $ # 重命名 session
<prefix> + ( # 跳到上一個 seesion
<prefix> + ) # 跳到下一個 seesion
##############################################################################
# Misc
##############################################################################
<prefix> + t # 顯示時鍾
<prefix> + : # 命令行
##############################################################################
# pane 同步
##############################################################################
:setw synchronize-panes # 打開(關閉) pane 同步模式, 發送命令到所有的 pane 中
# 只影響當前 window 的 pane
##############################################################################
# 復制模式 (copy-mode)
##############################################################################
添加下面一行到 $HOME/.tmux.conf, 通過 vim 的快捷鍵實現瀏覽, 復制等操作;
setw -g mode-keys vi
更多 vim 快捷鍵可參考 ../editors/vim.txt, 以下列出一些常用快捷鍵.
<prefix> + [ # 進入 copy mode
vi emacs 功能
^ M-m # 跳轉到一行開頭
Escape C-g # 放棄選擇
k Up # 上移
j Down # 下移
h Left # 左移
l Right # 右移
L # 最后一行
M M-r # 中間一行
H M-R # 第一行
$ C-e # 跳轉到行尾
: g # 跳轉至某一行
C-d M-Down # 下翻半頁
C-u M-Up # 上翻半頁
C-f Page down # 下翻一頁
C-b Page up # 上翻一頁
w M-f # 下一個字符
b M-b # 前一個字符
q Escape # 退出
? C-r # 往上查找
/ C-s # 往下查找
n n # 查找下一個
Space C-Space # 進入選擇模式
Enter M-w # 確認選擇內容, 並退出
<prefix> + ] # 粘貼選擇內容(粘貼 buffer_0 的內容)
:show-buffer # 顯示 buffer_0 的內容
:capture-buffer # 復制整個能見的內容到當前的 buffer
:list-buffers # 列出所有的 buffer
:choose-buffer # 列出所有的 buffer, 並選擇用於粘貼的 buffer
:save-buffer buf.txt # 將 buffer 的內容復制到 buf.txt
:delete-buffer -b 1 # 刪除 buffer_1
##############################################################################
# mouse-mode
##############################################################################
:setw -g mode-mouse on # 打開鼠標模式
:set -g mouse-select-pane on # 通過鼠標選擇 pane
:set -g mouse-resize-pane on # 通過鼠標更改 pane 大小
:set -g mouse-select-window on # 通過鼠標選擇 window
##############################################################################
# References
##############################################################################
https://gist.github.com/MohamedAlaa/2961058
https://tmuxcheatsheet.com/
所有的快捷鍵都有對應的 command, 參考:
http://man.openbsd.org/OpenBSD-current/man1/tmux.1
可以通過 $HOME/.tmux.conf 更改配置, 參考一些比較好的 tmux 配置:
https://github.com/gpakosz/.tmux
