[Linux] tmux 终端复用命令行工具

tmux 终端复用命令行工具

tmux 是一款终端复用命令行工具,通常用于 Terminal 的窗口管理.可以在终端软件重启后通过命令行恢复上次的 session.

安装运行

macOS 上使用 Homebrew 安装即可:

brew install tmux

在 shell 主进程下运行以下命令可以操作 tmux 会话:

tmux new -s foo # 新建名称为 foo 的会话
tmux ls # 列出所有 tmux 会话
tmux a # 恢复至上一次的会话
tmux a -t foo # 恢复名称为 foo 的会话,会话默认名称为数字
tmux kill-session -t foo # 删除名称为 foo 的会话
tmux kill-server # 删除所有的会话

快捷键

一般情况下 tmux 中所有的快捷键都需要和前缀快捷键 ⌃b 来组合使用.

窗格(pane)操作

  • % 左右平分出两个窗格
  • " 上下平分出两个窗格
  • x 关闭当前窗格
  • { 当前窗格前移
  • } 当前窗格后移
  • ; 选择上次使用的窗格
  • o 选择下一个窗格,也可以使用上下左右方向键来选择
  • space 切换窗格布局,tmux 内置了五种窗格布局,也可以通过 ⌥1 至 ⌥5来切换
  • z 最大化当前窗格,再次执行可恢复原来大小
  • q 显示所有窗格的序号,在序号出现期间按下对应的数字,即可跳转至对应的窗格

窗口操作

  • c 新建窗口,此时当前窗口会切换至新窗口,不影响原有窗口的状态
  • p 切换至上一窗口
  • n 切换至下一窗口
  • w 窗口列表选择,注意 macOS 下使用 ⌃p 和 ⌃n 进行上下选择
  • & 关闭当前窗口
  • , 重命名窗口,可以使用中文,重命名后能在 tmux 状态栏更快速的识别窗口 id
  • 0 切换至 0 号窗口,使用其他数字 id 切换至对应窗口
  • f 根据窗口名搜索选择窗口,可模糊匹配

会话操作

在 tmux 会话中,使用前缀快捷键 ⌃b 配合以下快捷键可操作会话:

  • $ 重命名当前会话
  • s 选择会话列表
  • d detach 当前会话,运行后将会退出 tmux 进程,返回至 shell 主进程

常见配置与问题

1、鼠标滚屏

在中.tmux.conf 配置文件增加以下内容:

开启鼠标模式

set -g mode-mouse on

允许鼠标选择窗格

set -g mouse-select-pane on

如果喜欢给窗口自定义命名,那么需要关闭窗口的自动命名

set-option -g allow-rename off

如果对 vim 比较熟悉,可以将 copy mode 的快捷键换成 vi 模式

set-window-option -g mode-keys vi

配置文件修改完成后,可以 tmux kill-server 重启所有 tmux 进程,或者在 tmux 会话中使用 ⌃b : 进入控制台模式,输入 source-file ~/.tmux.conf 命令重新加载配置。

2、复制

  • 方法 1:使用 ⌃b z 进入窗格全屏模式,鼠标选择文本的同时按住 option 键 ⌥,然后使用 ⌘c 进行复制;

  • 方法 2 配置文件增加以下配置后,ESC进入复制模式 v 选择文本 y 复制文本.

bind Escape copy-mode # 绑定esc键为进入复制模式                                                                                                                              
bind -T copy-mode-vi v send-keys -X begin-selection # 绑定v键为开始选择文本     
bind -T copy-mode-vi y send-keys -X copy-selection-and-cancel # 绑定y键为复制选中文本

我的.tmux.conf

# 修改 ctrl+b 前缀为 ctrl+a
set -g prefix C-a
unbind C-b
bind C-a send-prefix
# set-option -g prefix2 `
# 绑定重载 settings 的热键
bind r source-file ~/.tmux.conf \; display-message "Config reloaded.."

# 设置为vi编辑模式
setw -g mode-keys vi # 设置为vi编辑模式
bind Escape copy-mode # 绑定esc键为进入复制模式
bind -T copy-mode-vi v send-keys -X begin-selection # 绑定v键为开始选择文本
bind -T copy-mode-vi y send-keys -X copy-selection-and-cancel # 绑定y键为复制选中文本

set-option -g default-command 'exec reattach-to-user-namespace -l zsh'
bind-key -T copy-mode-vi 'y' send-keys -X copy-pipe-and-cancel 'reattach-to-user-namespace pbcopy'
# bind-key -T copy-mode-vi MouseDragEnd1Pane send -X copy-pipe-and-cancel "pbcopy"

# bind-key C-c run-shell 'tmux save-buffer - | reattach-to-user-namespace pbcopy'
# bind-key C-v run-shell 'reattach-to-user-namespace pbpaste | tmux load-buffer - \; paste-buffer -d'

# 设置window的起始下标为1
set -g base-index 1
# 设置pane的起始下标为1
set -g pane-base-index 1

#-- base --#
set -g default-terminal "screen-256color"
set -g display-time 3000
set -g history-limit 65535

# 鼠标支持
set-option -g mouse on
# 关闭默认窗口标题
set -g set-titles off

#-- bindkeys --#
unbind '"'
bind - splitw -v -c '#{pane_current_path}'
unbind %
bind | splitw -h -c '#{pane_current_path}'

bind c new-window -c "#{pane_current_path}"

# 定义上下左右键为hjkl键
bind -r k select-pane -U
bind -r j select-pane -D
bind -r h select-pane -L
bind -r l select-pane -R

# 定义面板边缘调整的^k ^j ^h ^l快捷键
bind -r ^k resizep -U 1 # upward (prefix Ctrl+k)
bind -r ^j resizep -D 1 # downward (prefix Ctrl+j)
bind -r ^h resizep -L 1 # to the left (prefix Ctrl+h)
bind -r ^l resizep -R 1 # to the right (prefix Ctrl+l)

# 定义交换面板的键
# bind ^u swap-pane -U
# bind ^d swap-pane -D

# bind e lastp 
# bind ^e last
# bind q killp

#bind '~' splitw htop
#bind ! splitw ncmpcpp
# bind m command-prompt "splitw 'exec man %%'"
# bind @ command-prompt "splitw 'exec perldoc -t -f %%'"
# bind * command-prompt "splitw 'exec perldoc -t -v %%'"
# bind % command-prompt "splitw 'exec perldoc -t %%'"
# bind / command-prompt "splitw 'exec ri -T %% | less'"

# 输出日志到桌面
# bind P pipe-pane -o "cat >>~/Desktop/#W.log" \; display "Toggled logging to ~/Desktop/#W.log"

#-- statusbar --#
set -g status-right-attr bright
set -g status-bg black
set -g status-fg yellow

# 设置状态栏高亮
setw -g window-status-current-attr bright
# 设置状态栏红底白字
setw -g window-status-current-bg red
setw -g window-status-current-fg white

# 设置状态栏列表左对齐
set -g status-justify left
# 非当前window有内容更新时在状态栏通知
setw -g monitor-activity on
set -g status-interval 1

#set -g visual-activity on

setw -g automatic-rename off
setw -g allow-rename off
# 最大化(默认为z,增加模拟的b指令)
# unbind b
# bind b run ". ~/.tmux/zoom"

# set -g status-keys vi

# plugin-manager
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

# plugins
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'

# load-plugins-without-manager
#run-shell ~/.tmux/tmux-resurrect/resurrect.tmux
#run-shell ~/.tmux/tmux-continuum/continuum.tmux

# plugins-settings
set -g @resurrect-strategy-vim 'session' # for vim
set -g @resurrect-strategy-nvim 'session' # for neovim
set -g @continuum-save-interval '180' # '0'
set -g @continuum-restore 'on'
set -g @continuum-boot-options 'fullscreen'

set -g status-right 'Continuum status: #{continuum_status}'
set -wg window-status-format " #I:#W "
setw -g window-status-current-format " #I:#W "
set -wg window-status-separator ""
set -g message-style "bg=#202529, fg=#91A8BA"

set -g @resurrect-capture-pane-contents 'on' # 恢复面板内容

run '~/.tmux/plugins/tpm/tpm'

猜你喜欢

转载自www.cnblogs.com/abeen/p/10460077.html