跳转至

Tmux

For Starter

  • What's Tmux?
  • Why do we need Tmux?
  • How to use Tmux?
  • How to make it look better?

Here we just list some common issues for starter.

You can refer to this page to learn.

Customize Tmux

After reading the page above, you can learn that:

We can customize our Tmux by creating/using a configuration file (~/.tmux.conf).

I will list some common customization here.

Prefix key

In Tmux, there is a prefix key to trigger some commands, like command in MacOS.

All commands in tmux are triggered by a prefix key followed by a command key.

The default prefix key is Ctrl+b, which is not convenient for me (MacOS). So I change it to Ctrl+a.

Bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# $ vim $HOME/.tmux.conf

# Set the prefix to Ctrl+a
set -g prefix C-a
# Remove the old prefix
unbind C-b
# Send Ctrl+a to applications by pressing it twice
bind C-a send-prefix

# :wq

Status bar

I prefer to use tmux-powerline to make it look fancy.

Please follow the steps to install this 3rd-party plugin and activate for your own Tmux Configure file.

Analogy

(1) TPM

TPM (Tmux Plugin Manager) is used to install and load tmux plugins.

(2) Reload TMUX environment so TPM is sourced

Just like source ~/.zshrc to reload zsh environment, you can refresh your tmux environment.

Bash
1
2
# type this in terminal if tmux is already running
tmux source ~/.tmux.conf

(3) Command Mode in Tmux

tmux command mode with prefix :

First open a Tmux session, then press Ctrl+a (or Ctrl+b), then press :.

Now you are in Command mode.

Common comands

Please follow this page first.

Summary for Session / Window / Pane

使用场景

Session(会话)适用于:

  • 完全分离不同的工作环境,如将工作项目和个人项目分开
  • 需要长期运行且相互独立的任务组
  • 不同项目之间需要 完全隔离 的情况

Window(窗口)适用于:

  • 同一个项目或任务中需要多个相关联的终端
  • 相同上下文 中切换不同的工作内容
  • 需要快速切换查看的相关任务

管理方式

Session管理:

  • 使用tmux new -s session_name创建新会话
  • 使用tmux attach -t session_name连接到指定会话
  • 使用Ctrl-b s在会话之间切换

Window管理:

  • 使用Ctrl-b c创建新窗口
  • 使用Ctrl-b n切换到下一个窗口
  • 使用Ctrl-b p切换到上一个窗口
  • 使用Ctrl-b number直接跳转到指定窗口

层级关系

  • Session是最顶层的组织单位,可以包含多个Window
  • Window是Session内的子单位,可以进一步划分为多个Pane(纯视图)
  • 一个Session中可以有多个相关的Window,而不同Session之间则完全独立

Splitting Panes

When you attach into a session (eg. Tmux 0), you can split the pane into two panes.

Split pane

just for the view, not for the real operation (no split for windows).

  • left and right: <ctrl+a> + %
  • up and down: <ctrl+a> + "

First we split the pane into two panes (left and right).

alt text

Notice that the cursor is on the right side, we split it into two panes (up and down).

Since cursor is on the right pane now, what we split is the original right pane.

alt text

Right now we’re trapped in the newly created pane. But we really really want to go back to the left one.

Easy peasy: Switching to a different pane uses the C-b shortcut, where is the arrow key pointing to the pane you want to switch to.

  • ←: <ctrl+a> +
  • →: <ctrl+a> +
  • ↑: <ctrl+a> +
  • ↓: <ctrl+a> +

Closing Panes

Closing a pane is as simple as closing a regular terminal session. Either type exit or hit Ctrl-d and it’s gone.

  • close a pane: ctrl+d

Creating Windows

Firstly, you need to create a session:

Bash
1
2
tmux 
# <ctrl+a> + c

Then use <ctrl+a> + c to create a new window.

Now we are good!

  • create a new window: <ctrl+a> + c
  • switch to the next window: <ctrl+a> + n
  • switch to the previous window: <ctrl+a> + p

alt text

Here we should take a look at 4:1.1 and 1* > zsh.

  1. a:b.c
    • a: session
    • b: window
    • c: pane
  2. 1* > zsh
    • 1: window index
    • >: current window
    • zsh: shell

Session Handling

If you’re done with your session you can either get rid of it by simply exiting all the panes inside or you can keep the session in the background for later reuse.

Session is the biggest unit in Tmux.

Bash
1
tmux ls

This will give you a list of all running sessions.

To connect to that session you start tmux again but this time tell it which session to attach to:

Bash
1
tmux attach -t 0

Note that the -t 0 is the parameter that tells tmux which session to attach to. 0 is the first part of your tmux ls output.

If you prefer to give your sessions a more meaningful name (instead of a numerical one starting with 0) you can create your next session using

Bash
1
tmux new -s database

This will create a new session with the name “database”.

Take-Away

This part is a summary of my prefer option in regular code-based development.

My task usually needs independent terminal sessions, so I use tmux to create multiple sessions.

Session level is all I need.

init

Bash
1
2
3
4
5
6
7
8
# init a empty session
tmux new -s session_name

# init a session with a command
tmux new-session -s session_name 'your_command'

# init a backend session
tmux new-session -d -s session_name

attach

Bash
1
tmux attach -t session_name

eg:

Bash
1
2
3
4
5
6
7
8
 tmux ls
0: 1 windows (created Sat Nov 30 10:37:16 2024)
1: 1 windows (created Sat Nov 30 11:09:47 2024)
2: 1 windows (created Sat Nov 30 11:09:59 2024)
3: 1 windows (created Sat Nov 30 11:10:10 2024)
4: 4 windows (created Sat Nov 30 11:48:28 2024)
test: 1 windows (created Sat Nov 30 12:04:46 2024)
test_bxhu: 1 windows (created Sat Nov 30 12:04:58 2024) (attached)

example in Unison

You don't need to care about this, it's just a recording for myself

Init:

Bash
1
2
tmux new-session -d -s dctcp_example 'time ./ns3 run dctcp-example > ./dctcp-example.txt 2>&1'
tmux new-session -d -s dctcp_example_mtp 'time ./ns3 run dctcp-example-mtp > ./dctcp-example-mtp.txt 2>&1'

Check for each session:

Bash
1
2
tmux attach-session -t dctcp_example
tmux attach-session -t dctcp_example_mtp