A Minimal Introduction to tmux

A Minimal Introduction to tmux

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

tmux.png

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/TextMeaning
[0]session name
0window label
zshwindow 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.

tmux_vertical_pane.png

To create a new horizontal pane, press ctrl+b and ".

tmux_horizontal_pane.png

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.

tmux_new_window.png

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 and l -> navigate to last window
  • ctrl+b and p -> navigate to previous window
  • ctrl+b and n -> 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.

tmux_rename_windows.png

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.

tmux_session_detached.png

Now, you are back to your normal terminal. To view all the tmux sessions running in the background, type:

tmux ls

tmux_session_ls.png

Symbol/TextMeaning
0session label
1number 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

tmux_session_state_preserve.png

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'

tmux_session_rename.png


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.

tmux_session_new.png

Keep in mind that these sessions will be preserved until the system reboots.


To kill git session, type:

tmux kill-session -t git

tmux_session_kill.png


tmux can do a lot more. In my next post on tmux, I will share its advanced usages.