Posted on July 8, 2023 by nexonhost
How To Use Tmux & How To Understand Tmux.
Tmux is a terminal multiplexer an alternative to GNU Screen . In other words, it means that you can start a Tmux session and then open multiple windows inside that session. Each window occupies the entire screen and can be split into rectangular panes.
This guide will go through the installation and basic usage of Tmux to get you up and running.
Installing Tmux on Ubuntu and Debian
sudo apt install tmux
Installing Tmux on CentOS and Fedora
sudo yum install tmux
Installing Tmux on macOS
brew install tmux
Starting Your First Tmux Session
To start your first Tmux session, simply type tmux in your console:
tmux
This will open a new session, create a new window, and start a shell in that window.
Once you are in Tmux you’ll notice a status line at the bottom of the screen which shows information about the current session.
You can now run your first Tmux command. For example, to get a list of all commands, you would type:
Ctrl+b ?
Creating Named Tmux Sessions
By default, Tmux sessions are named numerically. Named sessions are useful when you run multiple Tmux sessions. To create a new named session, run the tmux command with the following arguments:
tmux new -s first_session
It’s always a good idea to choose a descriptive session name.
Detaching from Tmux Session
You can detach from the Tmux session and return to your normal shell by typing:
Ctrl+b d
The program running in the Tmux session will continue to run after you detach from the session.
Re-attaching to Tmux Session
To attach to a session first, you need to find the name of the session. To get a list of the currently running sessions type:
tmux ls
The name of the session is the first column of the output.
0: 1 windows (created Sat Jul 8 11:17:35 2023) [117x29]
As you can see from the output, there are two running Tmux sessions. The first one is named 0 and the second one my_named_session.
For example, to attach to session 0, you would type:
tmux attach-session -t 0
Working with Tmux Windows and Panes
When you start a new Tmux session, by default, it creates a single window with a shell in it.
To create a new window with shell type Ctrl+b c, the first available number from the range 0…9 will be assigned to it.
A list of all windows is shown on the status line at the bottom of the screen.
Below are some most common commands for managing Tmux windows and panes:
Ctrl+b c Create a new window (with shell)
Ctrl+b w Choose window from a list
Ctrl+b 0 Switch to window 0 (by number )
Ctrl+b , Rename the current window
Ctrl+b % Split current pane horizontally into two panes
Ctrl+b “ Split current pane vertically into two panes
Ctrl+b o Go to the next pane
Ctrl+b ; Toggle between the current and previous pane
Ctrl+b x Close the current pane
Customizing Tmux
When Tmux is started, it reads its configuration parameters from ~/.tmux.conf if the file is present.
Here is a sample ~/.tmux.conf configuration with customized status line and few additional options:
~/.tmux.conf
# Improve colors set -g default-terminal 'screen-256color' # Set scrollback buffer to 10000 set -g history-limit 10000 # Customize the status line set -g status-fg green set -g status-bg black
Basic Tmux Usage
Below are the most basic steps for getting started with Tmux:
On the command prompt, type tmux new -s my_session,
Run the desired program.
Use the key sequence Ctrl-b + d to detach from the session.
Reattach to the Tmux session by typing tmux attach-session -t my_session.
Conclusion
In this tutorial, you learned how to use Tmux. Now you can start creating multiple Tmux windows in a single session, split windows by creating new panes, navigate between windows, detach and resume sessions, and personalize your Tmux instance using the .tmux.conf file.