Use a Terminal Multiplexer

Get you a tmux

I heard about GNU Screen for years, but aside from using it as a serial terminal to talk to an Arduino a handful of times, I never used it much, and I didn't really get the big idea.

Then one day a friend convinced me to try tmux, which is a lot like screen but more modern and capable in a number of ways. It took a while to sink in, but now I rarely open a shell anywhere that's not inside a tmux session. Why? Well, you should try it out. First, let's install tmux.

Download: file
sudo apt-get update
sudo apt-get install tmux

And start it up.

Ok, so now you have your regular terminal, but with a little green status bar at the bottom. So what? It kind of seems like you just opened another terminal inside your terminal (yo dawg), but what does this get you?

Persistent Multi-Terminal Sessions & Easy Multitasking

Here's the big idea: Your network connection can go away - you can even just close a terminal window whenever you feel like it - and the next time you connect to the remote machine, you just type:

Download: file
tmux attach

...and there's your old shell, plugging away, just the way you left it. You can even connect to the same session from a bunch of different machines at once, and see the same thing on all of those screens.

There are other useful features. You can open up multiple shells and cycle between them, and there's a way to search through the scrollback in a given buffer that comes in handy all the time.

But the main thing is that you can leave lots of programs running on the Pi and reconnect to them whenever you feel like it, and even switch between different computers or networks for accessing them.  You could, for example, start things while you have a monitor and keyboard plugged into the Pi, then reattach to them later over SSH. (Or vice versa.)

Keybindings

In order to get much real use out of tmux, you'll want to remember a handful of keybindings.

Ctrl-b

Prefix for most commands. To send a literal Ctrl-b, type it twice in a row.

Ctrl-b d

Detach from current tmux session. (Leaves tmux running in the background; you can reconnect at any time with tmux attach.)

Ctrl-b c

Create a new window.

Ctrl-b n

Go to next window.

Ctrl-b p

Go to previous window.

Ctrl-b ?

Show a list of shortcuts (hit q to quit out of this list).

Ctrl-b w

Display a list of windows which you can navigate with the arrow keys and switch to with Enter.

Ctrl-b %

Split the current window into two panes.

Ctrl-b o

Cycle through open panes.

In order to quit tmux, just type exit or press Ctrl-d in the shell like you normally would to logout. Once all open windows are closed, tmux itself will exit.

There's lots more. The tmux man page and this cheatsheet are both good references.

This guide was first published on Feb 24, 2015. It was last updated on Feb 24, 2015. This page (Use a Terminal Multiplexer) was last updated on Nov 14, 2019.