What is tmux?
tmux (not Tmux) is a command-line utility that allows to create and manage multiple terminals from a single screen. In technical terms, it is known as a terminal multiplexer.
How to use it?
On macOS, install tmux using Homebrew
brew install tmux
Based on your OS, you can install it using package managers like apt-get
, pacman
, dnf
, yum
, etc. Visit tmux on GitHub for installation instructions for other operating systems.
To get into tmux, type:
tmux
You will see the tmux screen with the highlighted green/lime coloured bar at the bottom. The left side of the highlighted bar indicates the list of your opened windows.
Right now, we have just one window which is indicated by 0:zsh*
.
Symbol/Text | Meaning |
[0] | session name |
0 | window label |
zsh | window name (editable) |
* | active window |
Panes
To create a new vertical pane, press ctrl+b
and %
(shift+5
). In tmux world, ctrl+b
is the prefix.
To create a new horizontal pane, press ctrl+b
and "
.
To switch between panes, press ctrl+b
and up/right/down/left
arrow key.
Windows
To create a new window, press ctrl+b
and c
.
As you can see, a new window was created which is indicated by 1:zsh*
.
The reason both windows have zsh
as window name is because I am using zsh
shell. It would have been bash
or fish
or any other shell if you were using one of them.
Currently, the active window is window 1
. If you want to switch back to window 0
, press ctrl+b
and 0
.
There are other ways to navigate among windows:
ctrl+b
andl
-> navigate to last windowctrl+b
andp
-> navigate to previous windowctrl+b
andn
-> navigate to next window
To rename a particular window, say 0:zsh*
, press ctrl+b
and ,
. Let's rename it to react.
To rename window labelled 1
, first switch to it using ctrl+b
and 1
and then press ctrl+b
and ,
. Let's rename it to angular.
To close down a window, just type exit
.
Sessions
tmux handles terminal processes in the form of sessions.
Whenever you start tmux by typing tmux
in the terminal, you are actually starting a new tmux session. A tmux session preserves your state i.e. whatever process is running within that session.
Suppose you lose a connection to a server that was connected through SSH using tmux for whatever reason. The next step is to ssh back into that server and attach to the tmux session. At this point, you will be exactly were you left off. All the processes that were running before you lost the connection would have been intact.
To detach from a session that you are working on, press ctrl+b
and d
. As you might have guessed, d
stands for detached.
Now, you are back to your normal terminal. To view all the tmux sessions running in the background, type:
tmux ls
Symbol/Text | Meaning |
0 | session label |
1 | number of windows opened |
To re-attach to session labelled 0
, type:
tmux attach -t 0
Let's output some data to demonstrate how it preserves the sate.
fortune | cowsay -f turtle | lolcat
Detach from the session and then re-attach to it using the aforementioned commands. You will find that the state is preserved.
Now, let's rename the session labelled 0
.
tmux rename-session -t 0 'todo-app'
To create a new session named git
, type:
tmux new -s git
Let's view all the sessions that are currently running after detaching from the git
session.
Keep in mind that these sessions will be preserved until the system reboots.
To kill git
session, type:
tmux kill-session -t git
tmux can do a lot more. In my next post on tmux, I will share its advanced usages.