tmux 使用

常用

tmux(terminal multiplexer)是一个终端多路复用器,它允许你在一个终端会话中运行多个终端会话,并在不同的 SSH 会话之间保持连接。

  1. 启动 tmux 会话:

    tmux new -s mysession
    

    这里 -s mysession 是可选的,用于命名你的会话。

  2. 分离(detach)会话:

    tmux 会话中,按下 Ctrl-b 然后按 d,这会将你从 tmux 会话中分离出来,而会话继续在后台运行。

  3. 列出 tmux 会话:

    tmux ls
    
  4. 重新连接到 tmux 会话:

    tmux a -t mysession
    
  5. 关闭会话:

    可以在 tmux 会话中输入 exit 来关闭会话,或者在 tmux 外部使用:

    tmux kill-session -t mysession
    

快捷键

所有快捷键都需要按下前缀 Ctrl-b 后使用。

  • 分离窗口:d
  • 创建新窗口:c
  • 切换窗口:n 切换到下一个窗口,p 切换到上一个窗口。
  • 分割窗格:% 垂直分割窗格," 水平分割窗格。
  • 切换到下一个窗格:o

配置文件

~/.tmux.conf:

set -g mouse on                          # 启用鼠标支持
set -g history-limit 50000               # 扩展 tmux 历史
set -g default-terminal "tmux-256color"  # 设置默认终端模拟器
set -g default-shell "/usr/bin/zsh"      # 设置默认 shell

Oh my tmux!

可以使用 Oh my tmux! 美化 tmux 界面。

安装

git clone --single-branch https://github.com/gpakosz/.tmux.git ~/.tmux
ln -s ~/.tmux/.tmux.conf ~
cp ~/.tmux/.tmux.conf.local ~

启用 Powerline 字体:

sed -e "s/^tmux_conf_theme_left_separator_main/#tmux_conf_theme_left_separator_main/" \
    -e "s/^tmux_conf_theme_left_separator_sub/#tmux_conf_theme_left_separator_sub/" \
    -e "s/^tmux_conf_theme_right_separator_main/#tmux_conf_theme_right_separator_main/" \
    -e "s/^tmux_conf_theme_right_separator_sub/#tmux_conf_theme_right_separator_sub/" \
    -e "s/^#tmux_conf_theme_left_separator_main/tmux_conf_theme_left_separator_main/" \
    -e "s/^#tmux_conf_theme_left_separator_sub/tmux_conf_theme_left_separator_sub/" \
    -e "s/^#tmux_conf_theme_right_separator_main/tmux_conf_theme_right_separator_main/" \
    -e "s/^#tmux_conf_theme_right_separator_sub/tmux_conf_theme_right_separator_sub/" \
    -i ~/.tmux.conf.local

禁用 C-a 前缀键:

sed -e "s/^# set -gu prefix2/set -gu prefix2/" \
    -e "s/^# unbind C-a/unbind C-a/" \
    -i ~/.tmux.conf.local

重载配置文件:

tmux source ~/.tmux.conf

如果需要自定义配置,请编辑 ~/.tmux.conf.local

image

使用

  • Ctrl-a 作为第二组合键前缀(Ctrl-b 前缀依然保留)
  • +:最大化窗格
  • m:切换鼠标模式
  • -:上下分割窗格
  • _:左右分割窗格

TPM

Oh my tmux! 预装了 TPM(Tmux Plugin Manager),可以用它来管理 tmux 插件。

  • I:安装插件
  • u:更新插件
  • Alt + u:删除未启用的插件

Tmux Resurrect

Tmux Resurrect 插件用来保存和恢复 tmux 窗格布局。

启用 Tmux Resurrect:

sed -i "s|^#set -g @plugin 'tmux-plugins/tmux-resurrect'|set -g @plugin 'tmux-plugins/tmux-resurrect'|" ~/.tmux.conf.local
  • prefix + Ctrl-s:保存
  • prefix + Ctrl-r:恢复

参考:

主题

dracula.omt

  1. 安装主题:

    git clone https://github.com/atgmello/dracula.omt.git ~/.config/oh-my-tmux/themes/dracula.omt
    ln -s ~/.config/oh-my-tmux/themes/dracula.omt/dracula.conf ~/.config/oh-my-tmux/themes/dracula.conf
    
  2. 启用主题:

    vim ~/.tmux.conf.local
    
    themes_folder='~/.config/oh-my-tmux/themes/'
    theme="dracula.conf"
    if "[ -d ${themes_folder} ]" "source ${themes_folder}${theme}"
    

Troubleshooting

Tmux 继承了不想要的环境变量

Tmux server 启动时会继承所有环境变量,并在后续的每一个新会话中都会设置这些环境变量。有时可以删除不需要的环境变量:

tmux setenv -gu MY_ENV

参考:tmux 环境变量 | 今天我学了什么

posted @ 2024-10-06 22:45  Undefined443  阅读(61)  评论(0)    收藏  举报