Tmux: A great Terminal Multiplexer

Wed 23 Mar 2022

I've always had a love/hate relation with terminal multiplexers. I've used Screen, Terminator, Kitty and tmux. tmux always sticked with me.

Why would you want to use a multiplexer? Well, you keep everything within one window. It's easier to access and manage. Multiplexers are great but have a slight learning curve. You really have to get used to all the functionality tmux has to offer and to be very frank, when I first started using a multiplexer I was only using a fraction of it's functionality due to the sheer amount of shortcuts there are.

It's a good practice to review your workflow every once in a while. To review the software you use and see if you can use it more efficiently. Tmux could be a good addition to your skill set.

Installing tmux

I'm on Manjaro on my main machine, however, your installing commands would not differ all that much. On most package managers the package name is just tmux.

# Installing tmux (Arch based)
rob@Rathalos ~ $ sudo pacman -S tmux

# Installing tmux (Debian based)
rob@Rathalos ~ $ sudo apt-get install tmux

After installing you can try tmux out by entering tmux in a console. You can choose if you like it and set it as your default terminal later.

To set tmux as your default terminal you can add the following to your .bashrc file:

[[ $TERM != "screen" ]] && exec tmux

Getting started with tmux

There are a few principles to keep in mind when using tmux that will make things easier. You have sessions that are basically instances of tmux. Windows are like workspaces that live inside an tmux instance. Then there are panes, these are your terminal sessions inside a window. tmux allows you to manage all these aspects. I will give you some of the basic commands and shortcuts you need to get started with tmux. It is a very powerful tool that allows for organizing all your terminal needs.

Managing windows

Windows are great to keep multiple things open and organise a few different sessions. I use this for example when keeping open a tail for logs, I will create a window dedicated with some tail -f <logfile>. This way I can quickly go over and see what is happening. Another would be resource management. Or simply some command terminals where you do the main stuff. Here is a list of the most common commands you will need:

# Keep in mind that you need CTRL+b to enter shortcut mode.
# When in shortcut mode you can do : to enter command mode
# To create a new session (dont need this probably)

# To create a new window
c  create window

# To go to the next/previous window
n  next window

p  previous window

# (Re)name current window
,  name window
:rename-window <name>

# Kill a window
&  kill window

# List all windows (of all tmux instances) and be able to switch between them
# You can also switch by pressing the listed number in front of the window name
w  list windows

New windows will be listed at the bottom of your tmux instance. The active window will be marked with an *.

Managing panes

Managing panes is actually not that hard but the shortcuts are not as intuitive as the window managing shortcuts. They literally stand for nothing and have been chosen at random (or so it seems).

# Split and create a new pane in the current window
# Both commands can be combined with the -l option that defines how many cells in length the pane is
%  vertical split
:split-window -h
:wplitw -h
:wplitw -hl 5

"  horizontal split
:split-window -b
:splitw -b
:splitw -bl 5

# Switches to the next pane
o  switch pane

# Kill current pane
x  kill pane

# Toggle focus/fullscreen pane
z  focus/fullscreen pane

# Shows a visual number above the panes
q  show pane numbers

# Resizing
Hold Ctrl + b and use Arrowkeys
# -D = Down
# -U = Up
# -L = Left
# -R = Right
:resize-pane -D 5
:resize-pane -U 5
:resize-pane -L 5
:resize-pane -R 5

You can also exit terminal sessions within a plane and that will result in the plane closing. Closing the last pane in a window will quit the tmux session.

Detach and attach sessions

You can detach sessions and attach them to different tmux instances. This can sometimes be useful when using programs like PHPStorm for example and when moving to another project. You can quickly detach your current terminal session and attach it in the new PHPStorm instance.

# Detach a session
d detach window

# Attach a session
w  list windows

Now get started...!

These are the basics to get started. There are some cheatsheets that list some of the same commands that are on here. Additionally, there is a built-in cheatsheet that you can conjure up using :list-keys or :list-commands.