tmux

What’s tmux?

tmux is a terminal multiplexer for Unix-based operating systems. It allows multiple terminal sessions to be accessed simultaneously in a single window. It’s useful for running more than one command-line program at the same time.

It can also be used to detach processes from their controlling terminals, allowing SSH sessions to remain active without being visible. Hence, with tmux you can detach from a session without losing that session’s state.

If you accidentally quit your terminal or the connection to the discovery is lost, the changes or work you have done can be retrieved back again. All you need to do is to run tmux and attach to the tmux session.

Important commands

Some important commands in tmux are:

Commands Syntax Purpose

tmux new

tmux new -s <session-name>

Creates a new session

tmux ls

tmux ls

Shows all the sessions

tmux attach

tmux at -t <session-name>

Attaches to the particular session

tmux detach

tmux detach

Detaches from the current session

tmux kill

tmux kill-session

Kills all the tmux sessions.

tmux new

This command helps to create a new tmux session.

Syntax: tmux new -s <session-name>

 tmux new -s test

Output:

tmux Output

The output shows a new command line and at the bottom of the screen, you can see the created session test which is highlighted in green color. Now, you can start writing your scripts in this new command line.

tmux list

This command lists all the created sessions by the user.

Syntax: tmux ls

tmux ls

Output:

test: 1 windows (created Tue Sep 29 17:59:50 2020) [204x54]

The above output shows the list of created sessions.

tmux attach

The attach command helps the user to rejoin the created session/ reconnect to the running process again.

Syntax: tmux at -t <session-name>

tmux at -t test

Output:

tmux Output

You can infer from the bottom of the screen that the user has been reconnected to the session test again.

It’s highly recommended to create a tmux session and then work on the discovery. If the terminal is closed unfortunately, you can go the running process again with the help of the tmux session.

tmux detach

tmux can be used to keep a process working in the background. You can detach from the current session by typing the below command:

Syntax: tmux detach

tmux detach

Your system will drop to the normal command line after running the above command and you will see an output that reads [detached].

You can also use the keyboard shortcut Ctrl + B + D to detach from the session. The tmux session can be controlled by pressing Ctrl + B , followed by a hotkey. Refer to the link → tmux Cheatsheet for different keyboard shortcuts that can be used in tmux.

tmux kill

To kill the created sessions, tmux kill-session command can be used.

Syntax: tmux kill-session

tmux kill-session

This kills all the created tmux sessions. To verify, run the tmux ls command again to find the list of created sessions. To kill a particular session, run the below command:

Syntax: tmux kill-session | kill-ses -t <session-name>

tmux kill-session -t test

On executing the above command, only the session test will be killed.

References

For more information on the tmux options and commands, refer to the tmux cheat sheet link → tmux Cheatsheet