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 |
|
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 |
|
(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).
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.
Navigating Panes¶
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
- ←:
<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 |
|
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
Here we should take a look at 4:1.1
and 1* > zsh
.
a:b.c
- a: session
- b: window
- c: pane
1* > zsh
- 1: window index
>
: current windowzsh
: 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 |
|
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 |
|
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 |
|
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 |
|
attach
Bash | |
---|---|
1 |
|
eg:
Bash | |
---|---|
1 2 3 4 5 6 7 8 |
|
example in Unison
You don't need to care about this, it's just a recording for myself
Init:
Bash | |
---|---|
1 2 |
|
Check for each session:
Bash | |
---|---|
1 2 |
|