When developing, maintaining servers, or running scripts, we often need to open multiple windows in the terminal for operations. However, if each window requires opening a new terminal, it not only wastes time but also makes operations chaotic. At this time, terminal multiplexing tools become very useful. Among them, tmux is one of the commonly used terminal multiplexing tools.
Installing tmux#
On Ubuntu, you can install tmux using the following command:
sudo apt-get install tmux
Starting tmux#
Starting tmux is simple; just enter the following command in the terminal:
tmux
This will open a new tmux session.
tmux Shortcuts#
One of the great features of tmux is its shortcuts, which allow us to use it more efficiently.
Here are some commonly used tmux shortcuts:
Ctrl+b "
: Create a new horizontal pane in the current windowCtrl+b %
: Create a new vertical pane in the current windowCtrl+b arrow key
: Switch to different panes in the current windowCtrl+b c
: Create a new windowCtrl+b ,
: Rename the current window.Ctrl+b number key
: Switch to the corresponding numbered windowCtrl+b d
: Detach from the current tmux sessiontmux attach
: Reconnect to the previous tmux session
More shortcuts can be viewed using the tmux list-keys
command.
tmux Configuration File#
The tmux configuration file is ~/.tmux.conf
. By modifying this configuration file, we can change tmux's default behavior and add custom shortcuts. Here are some commonly used configurations:
# Change Ctrl+b to Ctrl+a
set-option -g prefix C-a
# Start numbering windows and panes from 1
set-option -g base-index 1
setw -g pane-base-index 1
# Switch windows using the Alt key
bind-key -n M-h select-pane -L
bind-key -n M-l select-pane -R
bind-key -n M-j select-pane -D
bind-key -n M-k select-pane -U
# Create a new window with Ctrl+a c
bind-key C-a c new-window
How to Configure tmux to Start Automatically When Launching Alacritty Terminal in Arch Linux's i3wm#
-
Install tmux
On Arch Linux, you can install it using the following command:
sudo pacman -S tmux
-
Create a tmux Configuration File
Create a file named
.tmux.conf
in your home directory and add the following content:# Change Ctrl+b to Ctrl+a set-option -g prefix C-a # Start numbering windows and panes from 1 set-option -g base-index 1 setw -g pane-base-index 1
-
Modify the Alacritty Configuration File
Open the Alacritty configuration file
~/.config/alacritty/alacritty.yml
and add the following content:shell: program: /usr/bin/tmux args: - new-session
This will automatically start a new tmux session when launching the Alacritty terminal.
If you want to enter tmux immediately after starting Alacritty, you can add the following content:
shell: program: /usr/bin/tmux args: - new-session - -A
- The
-A
option means that if a tmux session already exists, it will connect directly to that session.
- The
-
Restart i3wm
Execute the following command to restart i3wm:
i3-msg restart
Then, when you start the Alacritty terminal, a tmux session will automatically start.
Conclusion#
tmux is a very useful terminal multiplexing tool that allows us to use the terminal more efficiently. By mastering commonly used shortcuts and modifying the configuration file, we can personalize our use of tmux. I hope this article is helpful to you!