tmux cheat sheet

Notes published the
5 - 6 minutes to read, 1163 words
Categories: scripting shell
Keywords: cheat sheet scripting shell tmux

Target audience

tmux is an acronym for terminal multiplexer.

It is a program that makes it possible to execute and switch easily between many programs in one terminal.

It also makes it possible to run programs in the background and reattach them to a different terminal (for example when a ssh connection is lost).

Note 📝
This is a cheat sheet; it is not accurate, or complete, and omits a lot of details. But it should be good enough for a quick start.

Why?

To organize your desktop better

I decided I wanted to give tmux a try because I noticed that my desktop sessions got more and more cluttered over time.

With half a dozen open terminals, some with interactive sessions to a remote machine, others executing specific jobs (compiling programs, tailing logs, …​), and others idling in specific working directories, I decided that I needed to find a way to organize my workplace better.

I have/had the (maybe not-so-good) habit of opening a new console to execute a single command (like opening a man page); this leads to an increasing number of open consoles. Maybe a tiling window manager would have helped (I doubt it), but it would also have meant changing my working habits with graphical programs, which would have been a little too much, as my main issue was with command line programs.

As of today tmux is one of the first programs I install on any machine I get my hand on, especially remote machines.

If you are using ssh

Especially when connecting remotely to another machine, for example with ssh, tmux is a real lifesaver.

Normally, when you disconnect from ssh, you either close your remote shell or detach from it.

In both cases, you won’t normally be able to connect with ssh and see the output of the previous commands, or the output of the program running when you detached your session (or the connection dropped).

With tmux, all of this is possible. The ssh connection can be closed at any time, and it is possible to create a new connection and simply continue to work.

Architecture Overview

Server and clients

When executing tmux, there are at least two programs running; a server and a client.

The server runs in the background and manages the programs. If the connection is lost, for example by closing the terminal by accident, the programs continue to run, and it is possible to reattach to the server, as if the terminal had never been closed.

Window and Panes

Every terminal opened inside tmux belongs to a pane, a pane is where the content of the terminal is shown.

A window is a collection of panes; it is possible to split it horizontally and/or vertically one or more to show the output of multiple terminals at once.

Starting and finishing a tmux session

Simply type tmux in your terminal to start a new session. If this is the first session, the server is started automatically for you.

Once the session is loaded, you can interact with your shell as if tmux would not be there.

To finish a session, it’s enough to finish the command that is running, in this case, the user shell, with Ctrl+D or exit.

Handle multiple terminals in one window

Create new panes

It is possible to split a window horizontally or vertically.

The preferred way to do it is by using the appropriate keybindings. Ctrl+B followed by % splits the current pane horizontally. For splitting vertically, use Ctrl+B+ followed by ".

Otherwise, it is possible to type in the pane one wants to split the appropriate command

  • tmux split-window -v to make a vertical split

  • tmux split-window -h to make a horizontal split

The keybindings have the advantage that they can be used while another program is already running.

Zoom and resize

Once you have multiple windows, it happens that you might want to "zoom" in a specific pane.

Ctrl+B followed by Z zooms the selected pane, using the same sequence also "zoom-out".

Warning ⚠️
If you are still pressing Ctrl while pressing Z (thus something like Ctrl+B+Z) you are sending the sequence Ctrl+Z, which is used for sending processes to the background. To bring the client into the foreground type fg.
Note 📝
Ctrl+Z inside tmux (ie not prefixed by Ctrl+B), works as expected; the command executed from the shell inside tmux is suspended, not tmux itself.

Other than zooming in and out, it is also possible to resize a pane.

Use Ctrl+B, keep Ctrl pressed, and the press an <arrow>, like for moving the vertical bar character.

It is also possible to press the arrow key more than once until the pane is of the desired size, like Ctrl+B++++.

Navigate

For navigating between panes, you can use the mouse, or Ctrl+B followed by an arrow (, , , and , but not PgUp🠕, PgDn🠗, , or ).

Do not keep Ctrl pressed, otherwise, you’ll resize the pane.

Using multiple windows

It is possible to create a new window with Ctrl+B followed by C. A new window is automatically created with a new pane.

In the bar at the bottom, it is possible to see all windows that have been created in the given session.

To navigate between windows, use Ctrl+B followed by W to show an interactive view of all window. Another possibility is to use Ctrl+B followed by a digit, or Ctrl+B followed by P for switching to the previous window and Ctrl+B followed by N for switching to the next window.

Conclusion

The biggest disadvantage of tmux is that there is no "native" version for Windows.

Thanks to Cygwin, it is possible to have a tmux.exe on Windows, even if it would use bash and not cmd or PowerShell as the default shell. This can be changed/customized, but at that point, I tend to prefer to use bash or a similar shell.

Some terminals provide support for tabs and split windows, it might be good enough, but it is something different. In particular, it will not help when connecting via ssh to a remote machine.

Windows excluded, the program is packaged for most if not all Linux distributions and BSD systems, making it available on nearly all environments I work.

Some might recommend GNU screen as it existed long before tmux, but defaults matter. While it is true that they have similar functionalities, tmux is (personal opinion) more user-friendly and easier to use than screen.

As there is no "native" GNU screen version for Windows either, I never have been in a situation where I could use screen but not tmux.


Do you want to share your opinion? Or is there an error, some parts that are not clear enough?

You can contact me anytime.