tmux安装插件管理器及日志记录插件(tmux-plugins & tmux-logging,附 tmux 备忘录)

安装前准备

  • 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

  1. 安装 tpm (tmux-plugins manager)
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
  1. 执行 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'
  1. 启动 tumx 并激活配置
$ tmux
#(会切换到 tmux shell)
$ tmux source ~/.tmux.conf
  1. 安装插件(一键安装)
  1. (如果已执行第2步,请忽略)向 ~/.tmux.conf 添加一行 with set -g @plugin 'xxx'xxx 为插件名称
  2. 按下 <prefix> + shift + itmux 将自动安装和配置插件到 ~/.tmux/plugins/ 目录
  1. 卸载插件(非必须
  1. ~/.tmux.conf 删除或注释掉 with set -g @plugin 'xxx'
  2. 按下 <prefix> + alt + utmux 将自动将插件移除

tmux-logging 的三种常用功能

  1. 日志记录

按下 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

  1. 日志截屏

按下 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

  1. 历史记录(最常用)

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
posted @ 2022-03-05 16:56  jumpspider  阅读(2315)  评论(0编辑  收藏  举报