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

基本用法参考这里

其他

posted @ 2020-09-07 13:21  hiyang  阅读(824)  评论(0编辑  收藏  举报