Tmux使用

创建新会话

tmux new -s Test

查看会话

tmux list-sessions

连接已有会话

tmux attach -t Test

tmux attach -t Test -r   #只读模式

断开会话

Ctrl + b, d

会话中的窗口操作

前缀按键,默认为Ctrl + b,以下命令,先按前缀键后再执行

命令功能
c创建新窗口
,重命名当前窗口
0-9切换窗口(下面有窗口编号,当前窗口后面有* )
( / p切换到前一个窗口
) / n切换到后一个窗口
d分离当前客户端
L切换会话
i显示当前窗口信息
%垂直分割窗口
"水平分割窗口
方向键切换窗格
空格调整窗格排列方式
{/}交换窗格
Ctrl + 方向键调整窗口分割线
x关闭当前窗格
z最大化当前窗格(窗口名称后会添加*Z)
t显示时间

Tmux插件

Oh My Tmux!

Pretty & versatile tmux configuration made with ❤️ (imho the best tmux configuration that just works)
Github:https://github.com/gpakosz/.tmux

默认没有~/.tmux.conf文件,Oh My Tmux是一套优化好的tmux配置。

  • 安装
    Requirements:
    tmux >= 2.1 running inside Linux, Mac, OpenBSD, Cygwin or WSL (Bash on Ubuntu on Windows)
    outside of tmux, $TERM must be set to xterm-256color
    To install, run the following from your terminal: (you may want to backup your existing ~/.tmux.conf first)
cd ~
git clone https://github.com/gpakosz/.tmux.git
ln -s -f .tmux/.tmux.conf
cp .tmux/.tmux.conf.local .

tmux-plugin-manager

Github:https://github.com/tmux-plugins/tpm
使用apt安装

apt install tmux-plugin-manager

安装路径为/usr/share/tmux-plugin-manager

使用apt或克隆源码后,编辑~/.tmux.conf文件,添加以下几行内容:

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

#Other examples:
# set -g @plugin 'github_username/plugin_name'
# set -g @plugin 'git@github.com/user/plugin'
# set -g @plugin 'git@bitbucket.com/user/plugin'

#Initialize TMUX plugin manager (添加到.tmux.conf文件最后一行)
run -b 'usr/share/tmux-plugin-manager/tpm'

tmux-logging

Github:https://github.com/tmux-plugins/tmux-logging

  • 使用TPM安装 (推荐该方式)
    编辑~/.tmux.conf文件,增加一行:
set -g @plugin '/tmux-plugins/tmux-logging'

然后进入Tmux,按prefix + I 刷新插件,tpm会自动下载该插件。

  • 使用:
  1. 记录日志
    Toggle (start/stop) logging in the current pane.
    快捷键:prefix + shift + p
    File name format: tmux-#{session_name}-#{window_index}-#{pane_index}-%Y%m%dT%H%M%S.log
    File path: $HOME (user home dir)
    Example file: ~/tmux-my-session-0-1-20140527T165614.log

  2. 截屏
    Save visible text, in the current pane. Equivalent of a “textual screenshot”.
    快捷键:prefix + alt + p
    日志文件名格式: tmux-screen-capture-#{session_name}-#{window_index}-#{pane_index}-%Y%m%dT%H%M%S.log
    文件保存路径: $HOME (user home dir)

NOTE: this functionality depends on the value of history-limit - the number of lines Tmux keeps in the scrollback buffer. Only what Tmux kept will also be saved, to a file.
Use set -g history-limit 50000 in .tmux.conf, with modern computers it is ok to set this option to a high number.

  1. 清除pane历史记录
    快捷键:prefix + alt + c

修改日志条数

#increase history size
set -g history-limit 10000

搜索、选择、复制

Ctrl-b + [,进入选择模式,可以设置VI的搜索快捷键进行搜索,按空格开始选择,按回车确认选择内容
Ctrl-b + ], 粘贴选择的内容

使用VI搜索快捷键

编辑~/.tmux.conf,添加一行内容:

# Use vim keybindings in copy mode
setw -g mode-keys vi

复制内容到系统剪贴板

# -- clipboard -----------------------------------------------------------------

# in copy mode, copying selection also copies to the OS clipboard
#   - true
#   - false (default)
# on macOS, this requires installing reattach-to-user-namespace, see README.md
# on Linux, this requires xsel or xclip

tmux_conf_copy_to_os_clipboard=true

支持鼠标模式

# start with mouse mode enabled
set -g mouse on

Xshell中,开启鼠标模式后,按住Shift点击邮件,调出Xshell的右键菜单。

配置文件

.tmux.conf

### Tmux 安装 对于不同操作系统,Tmux 的安装方式有所不同。在基于 Debian 或 Ubuntu 的系统上,可以使用 `apt` 包管理器来完成安装: ```bash sudo apt-get install tmux ``` 而在 CentOS 或 RHEL 系统,则应采用 `yum` 来执行相同的操作[^4]。 ### 基本命令概览 了解一些基础的 Tmux 指令有助于快速上手该工具。例如,要查看所有的快捷键映射以及它们所关联的具体操作,可以运行如下指令: ```bash tmux list-keys ``` 如果想获取完整的内置命令列表连同各自的参数说明,那么下面这条语句将会很有帮助: ```bash tmux list-commands ``` 另外,通过 `$ tmux info` 可以得到当前所有会话的状态详情;当修改过 `.tmux.conf` 文件之后想要使更改生效的话,只需简单地输入: ```bash tmux source-file ~/.tmux.conf ``` 这将立即应用新的设置而不必重启程序[^1]。 ### 会话管理常用命令 创建一个新的会话非常直观,只需要调用 `new` 子命令即可建立默认名的新实例或者指定特定的名字给新启动的会话: ```bash tmux new -s mysession ``` 查询现有会话可通过以下命令实现: ```bash tmux ls ``` 连接至某个已存在的会话也很容易做到: ```bash tmux a -t mysession ``` 至于结束某一会话或是整个服务器进程也有相应的控制手段可供选用[^5]。 ### 高级特性与插件支持 除了上述提到的基础功能之外,Tmux 还提供了丰富的扩展能力供用户自定义工作环境。比如借助于第三方插件——Tmux Resurrect,能够保存并恢复复杂的布局结构,极大地方便了日常开发流程中的场景切换需求。其安装过程涉及克隆仓库到本地目录下: ```bash mkdir ~/.tmux && cd ~/.tmux git clone https://github.com/tmux-plugins/tmux-resurrect.git ``` 综上所述,无论是初学者还是有一定经验的技术人员都能找到适合自己水平的学习切入点,在掌握这些核心概念和技术细节的基础上进一步探索更多可能性[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值