Tmux 使用笔记
本文最后更新于 372 天前,如遇到任何问题欢迎在评论区留言呀!

基本概念

server:server 是一个进程,在后台运行,负责管理 tmux 的状态信息、在 tmux 中运行的所有程序,并跟踪他们的输出。server 进程和下面提到的 client 的进程通过 socket 进行通信。

client:client 进程是接受用户输入和展示程序输出的终端窗口程序对应的进程。detach 关闭终端窗口后进程结束,attach 打开终端窗口进程创建。

panes:pane 就是图中显示的一块区域,一个 pane 下有一个 shell 的进程(例如 bash、zsh 等)负责处理用户的输入。

windows:window 类似于多桌面中的一个桌面。

sessions:多个 window 就组成了一个 session,同一个 window 可以归入不同的 session 进行管理。类似于网页,sesion 类似于收藏夹,window 类似于网页。

tmux 主要窗口示意图

操作 tmux:所有对 tmux 的操作(例如,切换 window、创建 pane 等)都是通过 tmux 所支持的命令实现的,无论是通过配置文件还是快捷键对 tmux 的操作,本质上都是通过 tmux 命令操作 tmux。

配置文件

关于 tmux 的配置文件:tmux 配置文件中的每一行都是一条可执行的 tmux 命令,例如配置文件中有一行set-option -g prefix C-j,其实就是在加载配置文件时,在命令行中执行了tmux set-option -g prefix C-j这条命令。

本文后续所有的命令均是基于下面的配置文件,因此,可能有些命令无法在默认配置上使用或者与默认配置有差异。

用户目录下的.tmux.conf中:

# tmux man page: https://linux.die.net/man/1/tmux

#==========================================================
# tmux behavior
#==========================================================

# configure 256 color
set-option -g default-terminal "tmux-256color"

# change default prefix to Ctrl-j
unbind-key C-b
set-option -g prefix C-j

# allow -r flag commands to be entered without pressing the prefix-key again in 1500ms
set-option -g repeat-time 1500

# enable mouse in tmux
set-option -g mouse on

# use vi mode in code mode
set-window-option -g mode-keys vi

#==========================================================
# key bindings
#==========================================================

# change keybinds for splitting windows
unbind-key %
bind-key | split-window -h
unbind-key '"'
bind-key - split-window -v

# add keybind for easily refreshing tmux configuration
unbind-key r
bind-key r source-file ~/.tmux.conf

# add keybinds for easily resizing tmux panes
bind-key -r j resize-pane -D 1
bind-key -r k resize-pane -U 1
bind-key -r l resize-pane -R 1
bind-key -r h resize-pane -L 1

# add keybind for maximizing and minimizing tmux pane
bind-key -r m resize-pane -Z

# start selecting text with "v"
bind-key -T copy-mode-vi 'v' send-keys -X begin-selection
# copy text with "y"
bind-key -T copy-mode-vi 'y' send-keys -X copy-selection
# don't exit copy mode after dragging with mouse
unbind-key -T copy-mode-vi MouseDragEnd1Pane

# set prefix-u to switch the last-window
bind-key u last-window

#==========================================================
# status bar
#==========================================================

# customize right status bar
set-option -g status-right '%m-%d %H:%M'
# refresh status bar every second
set-option -g status-interval 1

#==========================================================
# plugin
#==========================================================

# tpm plugin
set-option -g @plugin 'tmux-plugins/tpm'

# for navigating panes and vim/nvim with Ctrl-hjkl
set-option -g @plugin 'christoomey/vim-tmux-navigator'

# persist tmux sessions after computer restart
set-option -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @resurrect-capture-pane-contents 'on'

# automatically saves sessions for you every 3 minutes
set-option -g @plugin 'tmux-plugins/tmux-continuum'
set -g @continuum-save-interval '3'

# initialize tmux plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'

快捷键

快捷键在本质上是将快捷键与 tmux 的命令进行绑定。例如,在上面的配置文件中有一行bind-key u last-window,他就是将prefix+u这组快捷键和last-window这个命令进行绑定,当你在键盘上写下prefix+u时,底层就是执行了tmux last-window这条命令实现对tmux的操作。

session 部分

新建 session

tmux new -s [会话名]

断开 session

prefix d

恢复 session

tmux attach -t [会话名]
# 或者 
tmux a -t [会话名]

切换 session

在 tmux 中使用

prefix s

交互式的展示和切换 session(可以通过上下或者编号选取)按x可以删除当前位置的 session。

重命名 session

prefix $

window 部分

新建 window

prefix c

切换 window

# 切换到下一窗口
prefix n 
# 切换到上一窗口
prefix p
# 切换到最近一次访问的窗口
prefix l
# 交互式切换 window
prefix w

删除 window

prefix &

重命名 window

prefix ,

pane 部分

新建 pane

# 竖直分割线
prefix |
# 水平分割线
prefix -

切换 pane

# 顺时针方向切换 pane
prefix o
# 上/下/左/右
Ctrl k/j/h/l

删除 pane

prefix x

布局

prefix alt 1 # 均匀竖直并排
prefix alt 2 # 均匀水平并排

FAQ

  • 为什么在服务器上使用 tmux 可以防止远程运行的程序中断?
    通过 SSH 连接到服务器,所运行的所有程序,都是 SSH 的守护进程sshd的子进程,当连接断开的时候,由于 ssh 对应的进程中断,所有的子进程都会中断。如果用 tmux,运行的程序是作为 tmux 的子进程存在的。
    父子进程的关系如下:
    仅通过 SSH:调用的程序 -> bash -> sshd -> systemd
    tmux:调用的程序 -> bash -> tmux server -> systemd

参考资料

  1. Tmux Getting Started
  2. Tmux man page
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇