tmux

Why tmux| Quick Start| Sessions| Detaching| Attaching| Ending A Session| tmux Organization| tmux Cheatsheet

Why tmux

tmux is a powerful tool that allows you to create multiple terminal instances (called “panes”) within one terminal window. Instead of constantly moving between directory locations or having multiple terminal windows open, tmux can handle all of this within one terminal window.

Here’s a quick look at what you can do:

You can also detach from a tmux session, leave all of your panes and windows going, and then reattach to the session later (even after logout and back in!) and everything is still running and exactly how you left it.

Quick Start

Open a terminal window, ssh to the CS computers, then run the tmux command:

tmux

Split the window into two side-by-side panes using Ctrl-b % (hold Ctrl key down, hit b key, then let up and hit Shift-5 (the % key):

Ctrl-b %

Move between panes using Ctrl-b and arrow keys:

Ctrl-b RightArrow
Ctrl-b LeftArrow

Exit any panes or the entire tmux session with Ctrl-d (Control-d):

Ctrl-d

Sessions

Each time you invoke the tmux command you create a new session. The neat thing about a session is that they will run until you explicitly kill them (or the machine turns off).

Detaching

If you want to leave your current session but still keep it running all you need to do is simply detach from the session. Use Ctrl-b d to detach from a session and leave it running:

Ctrl-b d

Note: That means hold down Ctrl and hit b, then let up on both and hit d.

Your session and its processes are still running, but your terminal is now free to do something else. You can even log off, go eat, then ssh back in (to the same lab computer) and re-attach to your still-running tmux session.

Attaching

You may have multiple sessions running at one time. To see all the running sessions type:

tmux ls

This will list all currently running sessions on your machine. Now that we have the list of sessions, we can attach to an existing session via session name or session number. The following two commands do the same thing:

tmux attach -t <num>     //note your 1st session number is 0
tmux attach -t session-name

Note: If you only have one session running, tmux attach will connect you to that session

Ending A Session

As mentioned above, nothing short of killing a session (or turning off the computer) will end a session. If you are in a current tmux session, exiting all of the panes and windows (use Ctrl-d) will end the session. If you are not attached to the session, but want to end it, type the following (after tmux ls):

tmux kill-session -t <num> 
tmux kill-session -t session-name

tmux Organization

Now that you understand the basics, lets quickly discuss some tips on how to organize your tmux sessions. As discussed above, each invocation of tmux creates a session, each session can be divided up into windows, and these windows have panes within them. This is shown in the picture below.

Image from https://danielmiessler.com

Note: for multiple tmux windows, you only see one window at a time, but you can jump back and forth between the tmux windows (not to be confused with regular terminal windows, where you are running tmux).

A good way to think of these elements is as follows:

By default you can split screens horizontally (new pane below) using Ctrl-b " or vertically (new pane to right) using Ctrl-b %:

Ctrl-b "     # new pane below current pane
Ctrl-b %     # new pane to right of current pane

To switch between the panes:

Ctrl-b (arrow keys)

You can also zoom in on panes:

Ctrl-b z     # another Ctrl-b z to unzoom

tmux has many more helpful hotkeys. To learn more please checkout the very comprehensive tmux cheatsheet. Alternatively, the following section has a small sample of helpful tmux hotkeys.

tmux Cheatsheet

Pane Handling

Description Keys
Split panes vertically Ctrl-b %
Split panes horizontally Ctrl-b "
Toggle last active plane Ctrl-b ;
Goto pane left Ctrl-b leftarrow
Goto pane right Ctrl-b rightarrow
Goto pane left Alt leftarrow
Goto pane right Alt rightarrow
Move pane left Ctrl-b {
Move pane right Ctrl-b }
Swap panes Ctrl-b o
Kill pane Ctrl-b x

Window Handling

Description Keys
New window Ctrl-b c
Previous window Ctrl-b p
Next window Ctrl-b n
Rename a window Ctrl-b ,

And see your .tmux.conf file for ways to customize tmux!


Back to SwatCS Help Docs