Using tmux

tmux helps you keep your programs alive even after disconnecting from the server, and works as follows. In this example we will be using tmux on the syvert0 server.

Cheat sheet

Command Description
Ctrl+b, c Create a new window
Ctrl+b, n Switch to the next window
Ctrl+b, p Switch to the previous window
Ctrl+b, [0..9] Switch directly to window number 0 to 9
Ctrl+b, l Switch back to the last (most recently shown) window
Ctrl+b, d Detach from tmux session
Puts tmux in the background
Ctrl+b, , (Control+b, comma)
Rename the current window
Ctrl+b, PageUp
Ctrl+b, ]
Switch to scroll-back mode. Exit by pressing Escape.
Ctrl+b, & Forcibly destroy the current window.
Use only if the window doesn't respond. It's better to close the window by running the logout or exit shell command or pressing Ctrl+d.
Ctrl+b, Ctrl+b Tmux usually "swallows" the Ctrl+b keypress. To send an Ctrl+b keypress to the program running inside tmux, press Ctrl+b twice.

Start a new tmux session

After logging in on the server, run tmux.

NOTE: Tmux must be run on the server. If you by mistake start tmux on your local computer instead of on the server, and then proceed to log in to the server inside your local tmux session, your computations will not be protected by tmux and may be interrupted in case of network problems.

The green status bar at the bottom of the window indicates that tmux is running:

Any computation you start from inside tmux will keep running even if you get disconnected.

Reconnect to a tmux session

The next time you connect to the server, you can reconnect to your tmux session by running tmux attach. The green status bar at the bottom will re-appear.

If tmux attach outputs no sessions, you don't have an existing tmux session (the server may have rebooted, or you didn't use tmux the last time you connected), and you can start a new tmux session by running tmux.

How to manage several tmux sessions

It is possible to have several tmux sessions running simultaneously. tmux ls will show a list of your currently running tmux sessions,

To choose which one tmux attach should reconnect to, run tmux attach -t SESSION (in this example, 0 or 1). Without the -t flag, tmux will reconnect to the most recently used session.

But don't do this. A better solution is to start a single tmux session, and open several command line shells inside the one tmux session. See next section.

How to run several programs in one tmux session

You can run several programs in one tmux session by opening several tmux windows inside your existing tmux session. Each tmux window runs its own shell and has its command prompt, just like a new Terminal window would.

To open a new tmux window, press Ctrl+b, c (mnemonic: "c" for "create). The green status bar at the bottom now shows that your session has two tmux windows:

  • Session (orange arrow): The tmux session number/name is shown inside the square brackets.
  • Window list (blue arrow): The tmux windows appear here, and are numbered from 0 and up.
  • Status information (yellow arrow): The hostname of the server is shown along with the current date and time.

To navigate between the different windows, use Ctrl+b, n to move to the next window, Ctrl+b, p to move to the previous, or Ctrl+b, N (where N is a digit) to jump directly to window number N.

To close a window, log out from the terminal by typing logout or exit or pressing Ctrl+d while the shell prompt is displayed.

For other navigation options, see the cheat sheet at the top of this page.

Symbol explanation: In the above example, both windows (0 and 1) are named bash. Behind the window name, some symbols are shown:

Symbol Meaning
* Denotes the current window.
- Marks the last window (previously selected).
! A bell has occurred in the window. Some programs ring the bell when errors occur.

iTerm2 integration

FIXME TODO

2022-10-27, Per Kristian Hove