Getting Started with Tmux
Tmux is a terminal multiplexer that allows you to create and control sessions. It is often used to run a command in the background and be able to close the terminal without stopping the process.
When you start Tmux, it initiates a session stored (by default) in your /tmp
directory. By closing the window, reopening a terminal, and running tmux attach
, you can retrieve your processes.
It is also possible to open multiple Tmux sessions at the same time. To do this, simply run tmux new -s {session name}
.
Once you have multiple sessions, you can list all sessions using tmux ls
and attach to a session using tmux attach -t {session number or name}
.
Opening Multiple Windows
As mentioned earlier, a session does not represent a single terminal. Let’s explore some shortcuts for managing windows.
For example, let’s start with ctrl+b
, followed by releasing the keys and then pressing c
(create).
This will create a new window:
- You can go back to the previous window using
ctrl+b
andp
(previous) - Conversely, you can go to the next window using
ctrl+b
andn
(next) - If you have too many windows, you can also target a specific number using:
ctrl+b {number}
.
Note that window names display the last executed command by default and can be modified using ctrl+b
and ,
.
It is possible to exit Tmux without killing the processes using ctrl+b
and d
(detach). To exit Tmux and kill the processes, simply use ctrl+b
and x
(kill).
Opening Multiple Panes
It is possible to open multiple panes (splits) within a window. To do this, use ctrl+b
and "
(double quote) to split the window horizontally. To split the window vertically, use ctrl+b
and %
(percent).
By combining the two, you can achieve a result like this:
To navigate between panes, simply press ctrl+b
followed by the corresponding arrow key.
You can use the mouse to navigate between panes by pressing ctrl+b
, then :
and set -g mouse on
. Alternatively, you can create a ~/.tmux.conf
file in your user directory and add the line set -g mouse on
to enable mouse support by default.
Cheatsheet
I won’t go into detail about all the commands, but here’s a summary of the most useful ones:
Tmux Shortcuts
Commandes de base
tmux new
: Create a new Tmux sessiontmux attach -t <session_name>
: Attach to an existing Tmux sessiontmux switch -t <session_name>
: Switch to a Tmux sessiontmux list-sessions
: List all Tmux sessionstmux detach
(orCtrl-b d
) : Detach from the current Tmux sessiontmux kill-session -t <session_name>
: Close a Tmux session
Prefix Commands
Ctrl-b
: Default prefix key (can be modified)Ctrl-b c
: Create a new windowCtrl-b n
: Go to the next windowCtrl-b p
: Go to the previous windowCtrl-b l
: Switch to the last used windowCtrl-b 0-9
: Go to a numbered windowCtrl-b &
: Close the current windowCtrl-b ,
: Rename the current window
Window Splitting Commands
Ctrl-b %
: Split the window verticallyCtrl-b "
: Split the window horizontallyCtrl-b arrow key
: Navigate between panesCtrl-b space
: Toggle pane layoutCtrl-b z
: Toggle pane fullscreenCtrl-b {
: Move the active pane to the leftCtrl-b }
: Move the active pane to the rightCtrl-b Ctrl-arrow key
: Resize the active pane
Other Useful Commands
Ctrl-b ?
: Display the list of available commandsCtrl-b :
: Enter command modeCtrl-b d
: Detach from the current sessionCtrl-b t
: Display the clockCtrl-b [
: Enter copy mode (navigate with arrow keys, space to start selection, enter to copy)Ctrl-b ]
: Paste the previously copied textCtrl-b !
: Move the current window to a new sessionCtrl-b $
: Rename the current session
Customization
You can customize Tmux using a configuration file called .tmux.conf
located in your user directory.
For example, you can modify the Tmux prefix using the following line:
set-option -g prefix C-Space
Thus, ctrl+b
is replaced with ctrl+space
.
To reload the configuration, simply run tmux source ~/.tmux.conf
directly in Tmux.
You can then add custom shortcuts, for example:
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D
Which allows navigating between panes using alt+[arrow key]
.
Going further with plugins
There are many plugins available for Tmux that can be found on GitHub.
For example, it is possible to install a Dracula plugin to make the Tmux interface look nicer:
To do this, you will need to install Tmux Plugin Manager:
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
Next, place the following lines in your .tmux.conf
file:
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible' # plugin that adds default options, optional
set -g @plugin 'dracula/tmux' # Dracula plugin
run '~/.tmux/plugins/tpm/tpm' # this line should be the last line of the file
Then refresh the Tmux configuration using tmux source ~/.tmux.conf
and install the Dracula plugin using ctrl-b I
(It’s an uppercase i).
Already installed "tpm" [0/0]
Already installed "tmux"
TMUX environment reloaded.
Done, press ENTER to continue.
And after restarting Tmux, you should have a result like this:
Well, the weather and seeing my computer’s battery is nice, but those are not information that I need to see constantly. I propose these settings that display my CPU usage, used RAM, main network interface, latency, and Kubernetes context:
set -g @plugin 'dracula/tmux'
# available plugins: battery, cpu-usage, git, gpu-usage, ram-usage, network, network-bandwidth, network-ping, attached-clients, network-vpn, weather, time, spotify-tui, kubernetes-context
set -g @dracula-plugins "cpu-usage ram-usage network network-ping ram-usage, network-bandwidth, kubernetes-context"
set -g @dracula-show-empty-plugins false
## available colors: white, gray, dark_gray, light_purple, dark_purple, cyan, green, orange, red, pink, yellow
set -g @dracula-cpu-usage-colors "red dark_gray"
set -g @dracula-ram-usage-colors "dark_purple dark_gray"
set -g @dracula-network-colors "light_purple dark_gray"
set -g @dracula-network-ping-colors "yellow dark_gray"
set -g @dracula-kubernetes-context-colors "cyan dark_gray"
Result:
Conclusion
Tmux is a very powerful tool that allows you to manage sessions and panels, and can be customized endlessly. It is very useful for people who work on remote servers, but also for developers who need to run multiple commands at the same time.
Personally, I use it every day, and Tmux is one of the first tools I install on my machines.