tmux, short for terminal multiplexer, is a command line utility that makes working from the terminal much easier. It allows you to split your terminal window into many panes, which will persist across multiple SSH sessions.
Installing and Using tmux
tmux can be installed from the package manager for most distros. For Debian-based systems like Ubuntu, that would be:
apt-get install tmux
It’s also available on brew, a third-party package manager for macOS, with
brew install tmux.
Once it’s installed, you can get started by entering the
tmux command. However, this starts a session with a random name, so you can create a new named session with
tmux new -s [name]
This will take over your terminal window, and you’ll see a new command bar at the bottom. You’re now running inside
tmux, in a newly created session. This session acts as if you opened a new terminal window or a new SSH session and left it running on your desktop, except it’s running without a window and behind the scenes.
tmux allows you to connect to it. In essence,
tmux is a desktop environment for the terminal world.
If you close the actual terminal window, this
tmux session will persist until you manually terminate it (or restart your system). It works the same way when connecting to a remote server; everything you run will persist until you terminate the session.
To terminate the session, you can run the
exit command, or press Control+A, Control+D. You’ll see “exited” in your main terminal as tmux exits.
More often though, you’ll simply want to disconnect from the session and leave it running on the server. To do this, you’ll want to press the
tmux prefix shortcut, which is Control+B by default, and then press the “D” key. You’ll see “Detached From Session” in your main terminal when tmux exits.
To reconnect to a session, use:
tmux a -t [name]
a” command is short for
attach-session, which saves some keystrokes. Additionally, you can use the
# shortcut to connect to the last created session:
tmux a #
To view all sessions, you can run:
Which will display the session name and current number of windows. Make sure you’re not already connected to a
tmux session when trying to connect to another session, as recursion is blocked by default (and is a pain anyway).
Multitasking with Panes
Panes make heavy use of the
tmux prefix shortcut, so it’s best to remember it:
Every command will be prefixed with this so that
tmux intercepts it.
To make a new pane, use one of two shortcuts:
Control+B %to split vertically
Control+B "to split horizontally
These will split the current pane however you choose.
To switch between panes, you’ll have enter Control+B followed by an arrow key. You can also install mouse support with Oh My Tmux!, which will let you click between them.
If you aren’t satisfied with how big the panes are, you can resize them with these two commands:
Control+B : resize-pane -R 4
Control+B followed by a colon opens up the
tmux command prompt, which accepts more detailed commands. The command we use here is
resize-pane, which takes a direction (U for up, L for left, etc.) and the number of lines to expand. In this case, this command expands the currently selected pane four lines to the right.
If you don’t like the look of having your terminal split (or just want to multitask will full screen apps) you can use
tmux windows. You can use the following commands to work with windows:
Control+B cto create a new window
Control+B pto switch to the previous window
Control+B nto switch to the next window
Control+B 0-9to switch to a window by index number.
Control+B w will bring up an interactive exposé, allowing you to view what windows you have open, what commands they are running, and how many panes they have:
You can terminate a window at any time by using the
exit command, which will stop all running processes. Note that this will also stop
tmux altogether if you run it with only one window open.
If the window becomes unresponsive, you can force it closed with
Control+B &, which will always kill the running processes in that window.
tmux is wildly popular, and there’s plenty of plugins, themes, and community support behind it. Here’s a few useful ones:
There is a lot more to
tmux than is covered here, but the core functionality is relatively simple (which makes it a great utility).