Concepts of Sessions, Windows, and Panes#
#linux
tmux Common Commands and Shortcuts - Jian Shu
The main concepts in Tmux are divided into three parts:
-
Session - Session is the most important concept in Tmux. When Tmux is started, a session is automatically created, and within this session, a default window (also known as a pane) is opened. Tmux can have multiple sessions, and seamless switching between sessions is possible.
-
Window - Compared to sessions, windows are smaller units. Within a session, multiple windows can be opened. These windows belong to the same session and are managed by it.
-
Pane - Panes are smaller interface elements compared to windows. As mentioned earlier, Tmux allows for arbitrary division of windows, and the units divided from windows are called panes. Within the same window, users can control the cursor to move freely within the divided panes to select the currently active pane.
If these concepts are still not clear, perhaps the following diagram can clearly illustrate their relationship:
Their hierarchical relationship is: Session -> Window -> Pane
Installation and Configuration#
Tmux is very easy to install. Usually, for installation on Ubuntu, use:
sudo apt-get install tmux
On macOS, it is recommended to use brew for installation:
brew install tmux
Simple Configuration of Tmux#
After Tmux is installed, it can be started immediately. However, this is just a "bare" Tmux because many configuration options are set to their default values and need to be adjusted according to personal preferences. Starting from scratch to create a Tmux configuration can be quite troublesome, so it is recommended to find configuration files from other experts online and modify them, which is more convenient. Here is a configuration file that can be used as a template. Download it and modify it according to your preferences.
It should be noted that the configuration file of Tmux itself is a hidden file, which means it starts with a .
and is called .tmux.conf
. This file needs to be placed in the default home directory of the current user, that is, ~/.tmux.conf
. After Tmux is started, it will automatically read and load the configuration items in it.
With the configuration file done, if you are new to Tmux, it is recommended not to worry too much about understanding the meaning of each item in the configuration file. In the subsequent use, you may continuously modify and adjust it, and naturally become familiar with it. Next, let's take a look at the powerful shortcut functions of Tmux.
Here is a Tmux configuration file that I often use. It can be used as a template. Download it and modify it according to your preferences.
Tmux Usage Effect#
Common Shortcuts#
When using Tmux for the first time, it is important to understand that various shortcut functions of Tmux require a <prefix>
key combination as a prefix. In other words, you need to press the <prefix>
key combination first to activate the shortcut function, and then press the corresponding shortcut key for various functions to take effect. The reason for this design is probably to distinguish the input of shortcut keys from normal user input.
When using Tmux for the first time, we need to know that the default <prefix>
key combination prepared by Tmux is C-b
(Ctrl+b). Of course, according to our own habits, if we find it inconvenient to press C-b
, we can customize our own <prefix>
key combination by modifying the configuration file. For example, I find that C-b
is a bit far, so I define my <prefix>
like this:
set -g prefix C-j
Here is a brief list of some Tmux shortcut operations. Some of the shortcut keys may be different from the defaults provided by Tmux because I have made some adjustments based on my preferences.
Window Operations#
<prefix> c Create a new window
<prefix> w List all windows
<prefix> n Next window
<prefix> p Previous window
<prefix> f Find window
<prefix> , Rename the current window
<prefix> & Close the current window
Pane Operations#
<prefix> | Split vertically
<prefix> - Split horizontally
<prefix> o Swap panes
<prefix> x Close pane
<prefix> <space> Switch layout
<prefix> q Display the number of each pane. Press the corresponding number to select the pane.
<prefix> { Swap position with the previous pane
<prefix> } Swap position with the next pane
<prefix> u Toggle pane zoom
<prefix> n Toggle pane minimize
<prefix> 0-9 Switch to the specified window number
Pane Selection#
<prefix> h Select the pane on the left
<prefix> j Select the pane below
<prefix> k Select the pane above
<prefix> l Select the pane on the right
Pane Resize#
<prefix> H Expand the current pane to the left by 3 cells
<prefix> J Expand the current pane downwards by 3 cells
<prefix> K Expand the current pane upwards by 3 cells
<prefix> L Expand the current pane to the right by 3 cells
Session Related#
<prefix> :new<Enter> Start a new session
<prefix> s List all sessions
<prefix> $ Rename the current session
These are just some of the commonly used Tmux shortcuts. Don't be overwhelmed by so many shortcuts when you are new to Tmux. As you use Tmux more and more, these shortcuts will eventually become muscle memory. As the saying goes, "The sword that is never used grows rusty," hehe.
Summary#
Tmux is truly a powerful tool in the terminal. When combined with iTerm2 and Vim on macOS, it can easily create an efficient development environment. Running Tmux on a server can also bring many conveniences to your daily operations work. So, keep the Tmux method in mind, and it will surely bring you great convenience and efficiency in the future.