Tmux

Outline:

  • Intro
  • Config
  • Hierarchy
  • Commands
  • Session Handling

Intro

tmux是最常用的Terminal Multiplexer

tmux expects you to know its keybindings, and they all have the form <C-b> x wheretmux has the following hierarchy of objects:

  • All commands in tmux are triggered by a prefix key followed by a command key (quite similar to emacs).By default, tmux uses C-b as prefix key
    • which means
      1. press Ctrl+b
      2. release Ctrl+b
      3. press command key
  • 默认的session_namewindow_name都是0开头的
  • Tmux最常见的用途是用来attach && detach shell session. 也就是在退出登陆服务器后使服务器上的程序继续运行. 由于tmux会把session数据保存在运行tmux的本机, 因此要在服务器而不是用户自己的电脑上开启tmux. (这对其它的同类型工具, 例如Zellij, 也是一样的.)

Config

  • 配置文件位于: ~/. tmux.conf , 没有则自己创建

    • 我使用了符号链接+dotfile:

      1
          
    • 参考这个人的配置

    • 参考

    • 也就是说,

Hierarchy

Sessions

a session is an independent workspace with one or more windows

  • tmux starts a new session.
  • tmux new -s NAME starts it with that name.
  • tmux ls lists the current sessions
  • Within tmux typing <C-b> d detaches the current session
  • tmux a attaches the last session. You can use -t flag to specify which

Windows

Equivalent to tabs in editors or browsers, they are visually separate parts of the same session

  • <C-b> c Creates a new window. To close it you can just terminate the shells doing <C-d>
  • <C-b> [window_name] Go to the specified window. Note they are numbered
  • <C-b> p Goes to the previous window
  • <C-b> n Goes to the next window
  • <C-b> , Rename the current window
  • <C-b> w List current windows

Panes

- Like vim splits, panes let you have multiple shells in the same visual display.

  • <C-b> " Split the current pane horizontally
  • <C-b> % Split the current pane vertically
  • <C-b> <direction> Move to the pane in the specified direction. Direction here means arrow keys.
  • <C-b> z Toggle zoom for the current pane
  • <C-b> [ Start scrollback. You can then press <space> to start a selection and <enter> to copy that selection.
  • <C-b> <space> Cycle through pane arrangements.
  • <C-b> x: close the pane

Session Handling

  • <C-b> d : detach current session

  • <C-b> D: choose which of your sessions you want to detach.

  • tmux ls: List existing sessions

  • tmux attach -t [session_name]: attach to the session you specified

    • -t : 指定attach的session
  • renaming session:

    • 新建一个session,指定其名字

      1
      tmux new -s [session_name]
    • rename your existing session:

      1
      tmux rename-session -t 0 database

Advanced commands

Kill tmux windows in a session

Kill all tmux windows whose window index is greater than 0. In practice, it keeps window 0 and deletes windows 1, 2, 3, etc.

1
tmux list-windows -F '#{session_name}:#I' | awk -F: '$2 > 1' | xargs -I{} tmux kill-window -t {}

Breakdown:

1
tmux list-windows -F '#{session_name}:#I'

Lists tmux windows and prints each one as:

1
session_name:window_index

Example output:

1
2
3
main:0
main:1
main:2

Then:

1
awk -F: '$2 > 0'

uses : as the field separator. For a line like:

1
main:2

awk sees:

1
2
$1 = main
$2 = 2

So $2 > 0 keeps only windows with index greater than 0.

Example:

1
2
3
main:0
main:1
main:2

becomes:

1
2
main:1
main:2

Finally:

1
xargs -I{} tmux kill-window -t {}

runs tmux kill-window for each remaining target.

So it effectively runs:

1
2
tmux kill-window -t main:1
tmux kill-window -t main:2

One important detail: tmux list-windows without -a only lists windows in the current session. To apply this across all tmux sessions, use:

1
2
3
tmux list-windows -a -F '#{session_name}:#I' \
| awk -F: '$2 > 0' \
| xargs -I{} tmux kill-window -t {}