tmux 簡單使用


tmux 是一個與 GNU screen 類似的程序,可作為后者的替代品使用。

安裝

# Ubuntu 或 Debian
$ sudo apt-get install tmux

# CentOS 或 Fedora
$ sudo yum install tmux

# Mac
$ brew install tmux

配置文件 $HOME/.tmux.conf

適用於mac 和 centos,ubuntu,其他操作系統未測試
tmux version >= 3.1, 版本不合適可以進行編譯,這里提供一個編譯好的 tmux-3.1-linux-x86-64

# ============================
# ===       Plugins        ===
# ============================

set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-copycat'
#set -g @plugin 'tmux-plugins/tmux-urlview'
#set -g @urlview-key 'u'
set -g @plugin 'tmux-plugins/tmux-resurrect'
# for darwin and ubuntu
set -g @plugin 'xamut/tmux-network-bandwidth'
# for centos
set -g @plugin 'tmux-plugins/tmux-net-speed'
set -g @plugin 'ayzenquwe/tmux-ping'


# ==========================
# ===  General settings  ===
# ==========================

set -g default-terminal "screen-256color"
# 命令回滾/歷史數量限制
set -g history-limit 20480
set -sg escape-time 0
set -g display-time 1500
set -g remain-on-exit off

# 啟用 UTF-8 編碼
setw -g utf8 on
set -g status-utf8 on

# 從 1 開始編號,而不是從 0 開始
set -g base-index 1
set -g pane-base-index 1

# 狀態欄的提示信息的持續時間
set -g display-time 3000

set -g set-clipboard on

# 取消默認的前綴鍵 C-b
# unbind C-b
# 設置新的前綴鍵
# set -g prefix C-a


# ================================================
# ===     Copy mode, scroll and clipboard      ===
# ================================================

# enable mouse
#set -g mouse-select-pane on
# 鼠標支持默認是關閉的,開啟鼠標后,支持復制,翻屏,切換面板,切換窗口,resize。
set -g -q mouse on

# Vim 風格的快捷鍵綁定
setw -g mode-keys vi
set -g status-keys vi


# =====================================
# ===    Appearence and status bar  ===
# =====================================
# 狀態欄顏色
set -g status-justify left
set -g status-bg black
set -g status-fg blue
set -g status-left-length 40
set -g status-right-length 80

### 用戶界面
# 通知方式
setw -g monitor-activity on
set -g visual-activity on

# 自動設置窗口標題
set -g set-titles on
set -g set-titles-string '#H:#S.#I.#P #W #T' # 窗口編號,程序名稱,是否活動

# 調整狀態欄
set -g status-interval 1    # 狀態欄刷新時間
set -g status-justify left  # 狀態欄列表左對齊
setw -g monitor-activity on # 非當前窗口有內容更新時在狀態欄通知

set -g status-bg "#1c1c1c"    # 設置狀態欄背景黑色
set -g status-fg blue       # 設置狀態欄前景黃色
set -g status-style "bg=#1c1c1c, fg=#FCCC31" # 狀態欄前景背景色

# for darwin and ubuntu
set -g status-left  "#[bg=#3a3a3a,fg=#bcbcbc]Session #S "
#set -g status-right "🚀 #{network_bandwidth} #[bg=#272c35,fg=#ff9601]Ping:#{ping} #[bg=#3a3a3a,fg=#bcbcbc] %H:%M "

# for centos
#set -g status-left  "#[bg=#3a3a3a,fg=#bcbcbc]Session #S "
set -g status-right "up #{upload_speed} down #{download_speed} #[bg=#272c35,fg=#ff9601] ping #{ping} #[bg=#3a3a3a,fg=#bcbcbc] %H:%M "

set -wg window-status-format " #I #W "           # 狀態欄窗口名稱格式
set -wg window-status-current-format " #I:#W#F " # 狀態欄當前窗口名稱格式(#I:序號,#w:窗口名 稱,#F:間隔符)
set -wg window-status-separator ""               # 狀態欄窗口名稱之間的間隔
set -wg window-status-current-style "fg=#d7fcaf,bg=#60875f" # 狀態欄當前窗口名稱的樣式
set -wg window-status-last-style "fg=#ffd75f"    # 狀態欄最后一個窗口名稱的樣式
set -g message-style "bg=#202529, fg=#91A8BA"    # 指定消息通知的前景、后景色

set -g pane-border-bg colour234
set -g pane-border-fg colour234
set -g pane-active-border-bg colour232
set -g pane-active-border-fg colour232

set -g message-attr bold
set -g message-fg colour223
set -g message-bg colour235

# ==========================
# ===   Key bindings     ===
# ==========================

bind -r Tab lastp # 選擇最后一個面板
bind -r l last-window

# 使用 Vim 風格的按鍵在窗格間移動
#bind h select-pane -L
#bind j select-pane -D
#bind k select-pane -U
#bind l select-pane -R

# 調整窗口大小, Alt or option + x
bind-key -r    M-Up resize-pane -U 10
bind-key -r  M-Down resize-pane -D 10
bind-key -r  M-Left resize-pane -L 10
bind-key -r M-Right resize-pane -R 10

# 較易於使用的窗格分割快捷鍵
bind | split-window -h
bind - split-window -v
unbind '"'
unbind %

# Kill pane/window/session shortcuts
bind x kill-pane
bind X kill-window
bind C-x confirm-before -p "kill other windows? (y/n)" "kill-window -a"
bind Q confirm-before -p "kill-session #S? (y/n)" kill-session

# Detach from session
bind d detach
bind D if -F '#{session_many_attached}' \
    'confirm-before -p "Detach other clients? (y/n)" "detach -a"' \
    'display "Session has only 1 client attached"'

# reload configuration ~/.tmux.conf
unbind r
bind r source-file ~/.tmux.conf; display-message "tmux config reloaded ok"

# Prompt to rename window right after it's created
set-hook -g after-new-window 'command-prompt -I "#{window_name}" "rename-window '%%'"'

# Rename session and window
bind t command-prompt -I "#{window_name}" "rename-window '%%'"
bind T command-prompt -I "#{session_name}" "rename-session '%%'"

# Hide status bar on demand
bind s if -F '#{s/off//:status}' 'set status off' 'set status on'

# create new window
# bind c new-window
# create new session
bind C new-session


# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'

使用步驟

# 1 編輯配置文件

# 2 安裝插件管理工具
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

# 3 安裝插件
prefix + I
prefix + U

# 4 安裝依賴
# 4.1 tmux-urlview
brew install urlview

# 4.2 tmux-network-bandwidth
macOS: brew install gawk coreutils
Linux: apt-get install gawk net-tools coreutils

常用快捷鍵

  • Alt/option + 上下左右,更改pane大小

  • s 顯示隱藏狀態欄

  • prefix + / 搜索復制

  • prefix + C-s/C-r 保存恢復session

基本用法參考這里

其他


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM