Skip to content
Daniel Nachun edited this page Mar 18, 2024 · 1 revision

Why use tmux?

Tmux is a terminal multiplexer, a very powerful tool for managing terminal sessions. Although it can be used locally, often the most important use case is over SSH, where you ideally should only need to log in once and then use tmux to manage multiple panes/windows. This configuration uses tmux over GNU screen, an older tool with a similar goal which has not seen much maintenance in recent years.

Basic usage

Prefix keys

Tmux uses a prefix key as a way to tell it to treat the keys pressed afterward as commands to tmux rather than as keys to pass through the terminal session. The default prefix is Ctrl+b - this has been remapped in this configuration to Ctrl+t to avoid clashing with the ctrl+b command used in zsh. Additionally some commands use the prefix Ctrl+u to avoid conflict with other key bindings. For both prefix keys, you must release the key before pressing the next one! Continuing to hold down the prefix key while pressing the next key is a common beginner mistake. Throughout this guide, when you see Ctrl+t,c, for example, this means to press and release Ctrl+t and then press c

Managing sessions

The first step to using tmux is to create a new session. If you simply run tmux on the terminal, you will automatically launch a new session. You can also use tmux new-session to launch a new session as well. Your session will end automatically when the last pane has been closed (usually by typing exit in the terminal session) or explicitly if you use the kill-session command. You can launch as many tmux session as you want, but using too many separate sessions can make it hard to keep track of what is in each session.

Also note that all tmux sessions are local to the machine they are running on. For usage on a personal computer, this does not really matter, but it is very important when using tmux on servers to keep track of which node you launched your session on in order to find it later. Do not launch tmux sessions inside of terminal sessions controlled by a job scheduler - the background tmux server will be killed when the job ends. Instead, launch the tmux session on a login node that allows your processes to run indefinitely, typically with very limited resources (which is fine because tmux is a very small lightweight program).

Detaching sessions

One of the most useful features of tmux is the ability to detach sessions so that the processes running inside of them will continue to run as long as the computer they are running on remains powered on, even if you are not logged in.

  • To detach a running session from inside tmux, use Ctrl+t,d.

Your session is now detached. To reattach the session, use tmux attach-session from the command line. If you have more than one session, you will have to specify the session number with -t (for example tmux attach-session -t 2).

Panes and windows

When you have launched a session, you will start with one pane in one window. If you need more terminal sessions, you can either make a new pane or a new window. What tmux calls panes we might typically consider analogous to new windows on a desktop, and what tmux calls windows is more similar to tabs in many modern applications such as web browsers.

Managing panes

Pane management is largely handled by the tilish plugin. Please visit the linked page for more details on additional features not described here.

  • To create a new pane, Ctrl+u,Return - this will create a new pane that is horizontally split. To create another new pane, Ctrl+u,Return again, and this time the pane will be split vertically on the right side. This is called a binary space partitioning and is one of the most efficient ways to manage space.
  • To move between panes, use Ctrl+u,h/j/k/l to move to the left, bottom, top, and right, respectively. These are the keys as the vim home keys, and that is done deliberately to create synergy in the shortcuts used in neovim and tmux.
  • To move the panes themselves, use Ctrl+u,H/J/K/L. This is most useful if you want to swap a small pane with a bigger pane because you realize you need more space to view something larger.

Managing windows

You will often use windows in a tmux session to either separate out unrelated tasks, or because you have too many panes in your existing windows and need additional ones.

  • To make a new window, use Ctrl+t,c
  • To move to an existing window, use Ctrl+t,NUMBER where NUMBER is the window number seen at the bottom of the status bar.

Visual mode

Tmux implements a basic visual mode analogous to neovim/vim. It allows for easy searching, text selection and copying.

  • To enter visual mode, press Ctrl+t,[
  • To select text while in visual mode, press v and then use the standard vim/neovim movement keys (see this section in the Neovim page).
  • To copy selected text to the system copy buffer, use y. Note that for this to work over SSH, you must have xclip installed on the remote host, an X server on the local host, and X forwarding must be configured correctly over SSH. This may eventually be changed to a Wayland-based system for Linux remote hosts in the future.

Command prompt

Tmux has a command prompt that allows you to issue it many other commands that do not have key bindings. You can access the command prompt with Ctrl+t,:

Plugins

Tmux supports plugins and in this configuration they are managed with tpm.

tmux-tilish

The most important features of tmux-tilish have already been described in the Managing pane section.

tmux-yank

The tmux-yank plugin is what enables tmux to copy to the system copy buffer.

tmux-menus

The tmux-menus plugin provides pop-up menus to allow the user to choose from many options in tmux. It can be triggered with Ctrl+t,\