Photo of David Winter

david winter

Tmux - the essentials

Tmux is a great tool for managing multiple terminal sessions and layouts. You can disconnect from a tmux session and then reconnect to it later and carry on where you left off. There is a vast amount you can configure with tmux, and many commands that can be used, and at first it can be intimidating to learn and some may fear a steep learning curve. But knowing only a handful of essential commands is enough to be productive day-to-day. I’ve been using tmux for the past two years or so and haven’t really ventured beyond these, and still, have found plenty of value out of it.

Sessions

If you’ve never run tmux before and have no open sessions, just type:

tmux

This will open a new window with a single pane filling the entire screen.

One of the features of tmux is the ability to detach from a running tmux session and then be able to reconnect to it at a later time with everything being as you’d left it.

To detach from the new session you’ve just created type ctrl + b and then d. You will now have been returned to your original command prompt.

You can see the tmux session that you were just connected to by running:

tmux ls

To reattach to it, type:

tmux a

Tmux prefix command

Let’s talk about the tmux prefix command; ctrl + b. All commands you run within a tmux session are initiated with this. You press this combination first, and then after, the command you want to run. From here on, we’ll reference the prefix with prefix rather than the full ctrl + b.

Windows and panes

When within tmux there are two basic concepts that you need to know about if you’re going to use it productively; windows and panes. You can think of windows as tabs within a tmux session. They are listed along the bottom of the tmux window. Each window can have multiple panes, which are different terminal prompts split across the window in various layouts.

Windows

Each window is prefixed with a number to represent it. Type ctrl + b and then c to create a new window. You’ll see it appear in the window list and it has been selected. You now have two windows, one represented by 0 and the other by 1.

You can switch between windows by typing prefix + 0 to jump to the first window. Just use the number index to choose which window you want to switch to.

When you start creating lots of windows, it becomes hard to remember what is within each. By default each window will be named with the default shell it is running. To rename, type prefix + , and then enter a new name, then hit enter. You’ll see the name reflected in the window list displayed along the bottom of the screen.

Panes

Within window 0, let’s create some panes. Type prefix + % to split the current window into two vertical panes. You’ll notice the right-hand pane currently has the terminal focus.

Type prefix + q and the number for each pane will briefly appear on the screen. This is how you will navigate between panes. The numbers will disappear after a second or so. Just push the combination again prefix + q and then type 0 immediately to switch focus back to the leftmost pane.

Now go back to the rightmost pane with prefix + q and then 1. We can split this pane in two horizontally by pressing prefix + ".

Press prefix + q again and you can now see we have three panes indexed with 0—2.

Sometimes it’s very helpful to zoom in on a single pane. A good example is when you want to copy and paste the contents of a pane that go over more than one line. To zoom press prefix + z. You’ll notice the pane expands to fill the entire window. Also a Z will appear in the window list along the bottom. To unzoom, just press prefix + z again.

Summary of commands

  • tmux create a new tmux session
  • tmux ls list any existing tmux sessions
  • tmux a reattach to the last open tmux session
  • ctrl + b the default tmux command prefix
  • prefix + d detach from current tmux session
  • prefix + c create a new window
  • prefix + 0-9 to switch to the numbered window
  • prefix + , rename the existing window
  • prefix + % split the current pane into two vertical panes, left and right
  • prefix + " split the current pane into two horizontal panes, top and bottom
  • prefix + q view numbered panes for current window
  • prefix + q, 0-9 switch to pane immediately after displaying pane numbers
  • prefix + z to zoom and unzoom