Creating Tmux sessions
To create a new Tmux session and attach to it, run the following command from the Terminal:
$ tmux
Or,
$ tmux new
Detaching from Tmux sessions
To detach from a current Tmux session, just press Ctrl+b
and d
. You don’t need to press this both Keyboard shortcut at a time. First press "Ctrl+b"
and then press "d"
.
Once you’re detached from a session, you will see an output something like below.
[detached (from session 0)]
Creating named sessions
If you use multiple sessions, you might get confused which programs are running on which sessions. In such cases, you can just create named sessions.
For example if you wanted to perform some activities related to web server in a session, just create the Tmux session with a custom name, for example “webserver” (or any name of your choice).
$ tmux new -s webserver
List Tmux sessions
To view the list of open Tmux sessions, run:
$ tmux ls
Attaching to Tmux sessions
You can attach to the last created session by running this command:
$ tmux attach
Or,
$ tmux a
If you want to attach to any specific named session, for example “myoffice”, run:
$ tmux attach -t myoffice
Or, shortly:
$ tmux a -t myoffice
Kill Tmux sessions
When you’re done and no longer required a Tmux session, you can kill it at any time with command:
$ tmux kill-session -t myoffice
To kill when attached, press Ctrl+b
and x
. Hit "y"
to kill the session.
You can verify if the session is closed with tmux ls
command.
To Kill Tmux server along with all Tmux sessions, run:
$ tmux kill-server
Be careful! This will terminate all Tmux sessions even if there are any running jobs inside the sessions without any warning.
When there were no running Tmux sessions, you will see the following output:
$ tmux ls no server running on /tmp/tmux-1000/default
Split Tmux Session Windows
Tmux has an option to split a single Tmux session window into multiple smaller windows called Tmux panes.
This way we can run different programs on each pane and interact with all of them simultaneously.
Each pane can be resized, moved and closed without affecting the other panes. We can split a Tmux window either horizontally or vertically or both at once.
Split panes horizontally
To split a pane horizontally, press Ctrl+b
and "
(single quotation mark).
Split panes vertically
To split a pane vertically, press Ctrl+b
and %
.
Split panes horizontally and vertically
We can also split a pane horizontally and vertically at the same time. Take a look at the following screenshot.
First, I did a horizontal split by pressing Ctrl+b
"
and then split the lower pane vertically by pressing Ctrl+b %
.
As you see in the above screenshot, I am running three different programs on each pane.
Switch between panes
To switch between panes, press Ctrl+b and Arrow keys (Left, Right, Up, Down).
Send commands to all panes
In the previous example, we run three different commands on each pane. However, it is also possible to run send the same commands to all panes at once.
To do so, press Ctrl+b
and type the following command and hit ENTER
:
:setw synchronize-panes
Now type any command on any pane. You will see that the same command is reflected on all panes.
Swap panes
To swap panes, press Ctrl+b
and o
.
Show pane numbers
Press Ctrl+b
and q
to show pane numbers.
Kill panes
To kill a pane, simply type exit
and ENTER
key. Alternatively, press Ctrl+b
and x
. You will see a confirmation message. Just press "y"
to close the pane.
Zoom in and Zoom out Tmux panes
Now, I am going to zoom the left pane which has top command running. To do so, switch to left pane and press and z. The tmux prefix command is Ctrl+b, so I type Ctrl+b followed by z to zoom the pane. It is that simple!
Note: To switch between panes, press Ctrl+b and Arrow keys (Left, Right, Up, Down).
Autostart Tmux Session On Remote System When Logging In Via SSH
To autostart Tmux session when connecting via SSH, edit your remote system’s ~/.bash_profile file:
$ nano ~/.bash_profile
If the file is not available, just create it.
And add the following lines in it:
if [ -z "$TMUX" ]; then tmux attach -t default || tmux new -s default fi
Save and close the file. Log out and log back into the remote systems. You will be landed into a new Tmux session named ‘default’.
Now, start any process inside the Tmux session and detach it from by pressing Ctrl+b followed by d. The process will keep running inside the Tmux session even after the network connection is dropped. Once the network connect is back, SSH into the remote system and you will see that the job is still running!!
Toggling Status Line
Turn it off:
$ tmux set-option status off
And, turn it on:
$ tmux set-option status on
You can also bind a key (t in this example) to toggle the status line on and off, as follows:
$ tmux bind-key t set-option status
Open a terminal and use the following command:
# tmux
There’s a difference between Session vs Window vs Pane (split) in tmux
Windows:
Create windows using:
[ctrl + b] + c
Switching windows in a tmux session
Switch using window list
Another option uses [ctrl+b] + w
Name your current window using:
[ctrl+b] + ,
Name your tmux session using:
tmux new -s sessionname
If you already created a session, rename it using:
[ctrl+b] + $
Detach a session (come out of tmux session but keep it running in background)
use:
[ctrl + b] + d
List tmux sessions:
tmux ls
Reenter a session in Tmux (also known as attach)
tmux attach -t session_name_or_number
Creating multiple panes in tmux
Horizontal (Left/Right) | Vertical (Up/Down) |
[ctrl + b] + % | [ctrl + b] + “ |
Switching between panes
[ctrl+b] + arrow keys
Zoom into or out of a pane
[ctrl+b] + z
Killing or closing panes
[ctrl + b] + x
Resize panes
This assumes that you’ve hit ctrl + b and : to get to the command prompt
- :resize-pane -L
Here are some additional tmux pane resizing examples:
:resize-pane -D (Resizes the current pane down)
:resize-pane -U (Resizes the current pane upward)
:resize-pane -L (Resizes the current pane left)
:resize-pane -R (Resizes the current pane right)
:resize-pane -D 10 (Resizes the current pane down by 10 cells)
:resize-pane -U 10 (Resizes the current pane upward by 10 cells)
:resize-pane -L 10 (Resizes the current pane left by 10 cells)
:resize-pane -R 10 (Resizes the current pane right by 10 cells)
Killing sessions in tmux
tmux kill-session -t session_name_or_number
Kill all tmux sessions
tmux kill-server
Command | Explanation |
tmux kill-session -t name | Kill the mentioned session |
tmux kill-session -a | Kill all sessions except the last used session |
tmux kill-server | Kill all tmux sessions |
tmux attach -t name | Attach/enter the mentioned session |
[Ctrl +b] + d | Detach/exit the current session |
[Ctrl +b] + $ | Name or rename current session |
[Ctrl +b] + c | Create new window |
[Ctrl +b] + , | Name or rename current window |
[Ctrl +b] + w | List all windows |
[Ctrl +b] + & | Kill the current window |
[Ctrl +b] + n/p/N | Move to next/previous/Nth window |
[Ctrl +b] + % | Create horizontal pane |
[Ctrl +b] + “ | Create vertical pane |
[Ctrl +b] + space key | Toggle the layout of current pane |
[Ctrl +b] + z | Zoom into the current pane |
[Ctrl +b] + x | Kill the current pane |