其他分享
首页 > 其他分享> > Tmux 配置

Tmux 配置

作者:互联网

Tmux 配置

前面提到的窗口管理只是 tmux 功能的一小部分,另一个很有用的功能就是,连接到远程主机之后,一旦断开,那么当前账户登录的任务就被取消了,但是使用 tmux 可以在断开之后继续工作,下次登录可以查看。其他的功能还有:

  1. 窗口切换,每个窗口里还可以分割面板
  2. 配置方便,可以使用脚本
  3. 类似 vim 的双层操作逻辑
  4. 复制粘贴缓冲区

安装的话也很简单,在 mac 下直接 brew install tmux(前提需要安装 homebrew),Debian 下则直接 sudo apt-get install tmux

​ (adsbygoogle = window.adsbygoogle || []).push({});

在终端中输入 tmux 就可以打开一个新的 tmux session,tmux 的所有操作必须先使用一个前缀键(默认是 ctrl + b)进入命令模式,或者说进入控制台,就像 vim 中的 esc。

基本操作

信息查询

窗口控制

先来看看在 tmux 之外如何进行控制

更常用的是在 tmux 中直接通过默认前缀 ctrl + b 之后输入对应命令来操作,具体如下(这里只列出输入默认前缀之后需要输入的操作):

基本操作

窗口操作

面板操作

因为 iTerm2 的支持,很多切换的操作可以直接用鼠标进行,非常方便。具体大家可以自己尝试一下。

配置

我们可以先进行一些简单的配置,修改 ~/.tmux.conf 即可,让整个使用更方便。

#-- base --#
set -g default-terminal "screen-256color"
set -g display-time 3000
set -g history-limit 10000
set -g base-index 1
set -g pane-base-index 1
set -s escape-time 0
set -g mouse on

#-- bindkeys --#
# split windows like vim. - Note: vim's definition of a horizontal/vertical split is reversed from tmux's
unbind s
bind s split-window -v
bind S split-window -v -l 40
bind v split-window -h
bind V split-window -h -l 120

# navigate panes with hjkl
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# key bindings for horizontal and vertical panes
unbind %
bind | split-window -h # 使用|竖屏,方便分屏
unbind '"'
bind - split-window -v # 使用-横屏,方便分屏

# swap panes
bind ^u swapp -U
bind ^d swapp -D
bind q killp
bind ^e last

unbind r
bind r source-file ~/.tmux.conf \; display "Configuration Reloaded!"

#-- statusbar --#
set -g status-justify centre
set -g status-left "#[fg=red]s#S:w#I.p#P#[default]"
set -g status-right '[#(whoami)#(date +" %m-%d %H:%M ")]'
set -g status-left-attr bright
set -g status-left-length 120
set -g status-right-length 120
set -g status-utf8 on
set -g status-interval 1
set -g visual-activity on
setw -g monitor-activity on
setw -g automatic-rename off

# default statusbar colors
set -g status-bg colour235 #base02
set -g status-fg colour136 #yellow
set -g status-attr default

# default window title colors
setw -g window-status-fg colour244
setw -g window-status-bg default
#setw -g window-status-attr dim

# active window title colors
setw -g window-status-current-fg colour166 #orange
setw -g window-status-current-bg default
#setw -g window-status-current-attr bright

# window title string (uses statusbar variables)
set -g set-titles-string '#T'
set -g status-justify "centre"
set -g window-status-format '#I #W'
set -g window-status-current-format ' #I #W '

# pane border
set -g pane-active-border-fg '#55ff55'
set -g pane-border-fg '#555555'

# message text
set -g message-bg colour235 #base02
set -g message-fg colour166 #orange

# pane number display
set -g display-panes-active-colour colour33 #blue
set -g display-panes-colour colour166 #orange

# clock
setw -g clock-mode-colour colour64 #green

# 修改进入命令模式按键
# remap prefix to Control + a
# set -g prefix C-a
# unbind C-b
# bind C-a send-prefix

版本2

# --------------------------------------------------- Tmux Config -----------------------------------------------------------

# --------------------------------------------------- prefix -----------------------------------------------------------
# 修改指令前缀
set -g prefix C-f #
unbind C-f # C-b 即 Ctrl+b 键,unbind 意味着解除绑定
bind C-f send-prefix # 绑定 Ctrl+f 为新的指令前缀

# 从tmux v1.6版起,支持设置第二个指令前缀
# set-option -g prefix2 ` # 设置一个不常用的`键作为指令前缀,按键更快些

# 添加载在配置文件指令为: r
bind r source-file ~/.tmux.conf \; display-message "Config reloaded.."


# --------------------------------------------------- 更改新增面板键 -----------------------------------------------------------
unbind '"'
bind - splitw -v -c '#{pane_current_path}' # 垂直方向新增面板,默认进入当前目录
unbind %
bind =  splitw -h -c '#{pane_current_path}' # 水平方向新增面板,默认进入当前目录

# --------------------------------------------------- 开启鼠标支持 -----------------------------------------------------------
# v2.1及以上的版本
set-option -g mouse on



# --------------------------------------------------- vim 风格 -----------------------------------------------------------
# 绑定hjkl键为面板切换的上下左右键

bind -r k select-pane -U # 绑定k为↑
bind -r j select-pane -D # 绑定j为↓
bind -r h select-pane -L # 绑定h为←
bind -r l select-pane -R # 绑定l为→

# 面板调整大小
# 绑定Ctrl+hjkl键为面板上下左右调整边缘的快捷指令

bind -r ^k resizep -U 10 # 绑定Ctrl+k为往↑调整面板边缘10个单元格
bind -r ^j resizep -D 10 # 绑定Ctrl+j为往↓调整面板边缘10个单元格
bind -r ^h resizep -L 10 # 绑定Ctrl+h为往←调整面板边缘10个单元格
bind -r ^l resizep -R 10 # 绑定Ctrl+l为往→调整面板边缘10个单元格

# 复制模式更改为 vi 风格
# 进入复制模式 快捷键:prefix + [

setw -g mode-keys vi # 开启vi风格后,支持vi的C-d、C-u、hjkl等快捷键

# --------------------------------------------------- 复制粘贴 -----------------------------------------------------------

# 复制模式向 vi 靠拢

#旧版本:
#bind -t vi-copy v begin-selection  # 绑定v键为开始选择文本
#bind -t vi-copy y copy-selection # 绑定y键为复制选中文本

# 新版本:
bind -T copy-mode-vi v send -X begin-selection # 开始复制
bind -T copy-mode-vi y send -X copy-selection # 复制剪切板
bind p pasteb # 绑定p键为粘贴文本(p键默认用于进入上一个窗口,不建议覆盖)




# --------------------------------------------------- 其他 -----------------------------------------------------------

#设置窗口面板起始序号
set -g base-index 1 # 设置窗口的起始下标为1
set -g pane-base-index 1 # 设置面板的起始下标为1
set -s focus-events on
set-window-option -g automatic-rename on
set-window-option -g monitor-activity on


# --------------------------------------------------- 状态栏 -----------------------------------------------------------

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 -g message-style "bg=#202529, fg=#91A8BA" # 指定消息通知的前景、后景色


# 自定义状态栏
set -g status-interval 1 # 状态栏刷新时间
set -g status-justify left # 状态栏列表左对齐
setw -g monitor-activity on # 非当前窗口有内容更新时在状态栏通知


# --------------------------------------------------- linux -----------------------------------------------------------

# set -g status-left "Zorn #W" # 状态栏左侧内容
# set -g status-fg yellow # 设置状态栏前景黄色
# set -g status-style "bg=black, fg=yellow" # 状态栏前景背景色

# set -g status-right 'zorn@machine #{continuum_status}' # 状态栏右侧内容
# set -g status-left-length 300 # 状态栏左边长度300
# set -g status-right-length 500 # 状态栏左边长度500

# set -wg window-status-current-style "bg=black" # 状态栏当前窗口名称的样式
# set -wg window-status-current-style "bg=red" # 状态栏当前窗口名称的样式
# set -wg window-status-last-style "fg=red" # 状态栏最后一个窗口名称的样式


set -g status-left "

标签:status,set,bind,配置,tmux,window,Tmux,pane
来源: https://www.cnblogs.com/xs-xs/p/16407377.html