Introduction to tmux
Why tmux?
Let's face it - you're a busy person. When you surf the net, you have at least five tabs open. In one tab, you have Debussy's Clair de lune playing as background music, while in another, you have 10 messages from Facebook friends who want to catch up with you. And then you're on Reddit, get up-to-date with the latest tech news and cat memes. And finally, here, you're studying to be a pro-Linux users so you can swoon the ladies. Modern day browsers allow you to multi-task and organize your workspace within the same windows - how convenient!

But does the terminal offer similar features? With tmux
(short for terminal multiplexer), Yes! As defined on its official website, tmux
enables a number of terminals, each running a separate program to be created, accessed and controlled from a single screen.
Advantages of Using tmux
"But wait!" I hear you say, "I already have iTerm installed on my Mac OS X, which allows me to create new tabs and split window panes. Do I still need to learn tmux?" iTerm is great in that it gives you a slew of features for customizing and making the most out of the terminal. However, once you leave your precious Mac OS X (such as SSHing into a Linux machine), you've lost all your iTerm features.
Tmux, on the other hand, is completely portable, and its features are available from within any UNIX-like command line! Furthermore, there are other advantages of tmux
that trumps the use of a natively installed terminals.
- Multi-tasking: You can use one pane to display all current processes (using something like
top
), while other windows can be used to administer actual commands. - Availability:
tmux
is a shell program, meaning it can be installed and run directly within the command line. This allows you to runtmux
on remote servers. - Multiple interfaces: There's no need to open up multiple terminal interfaces and SSH into a host server from each one to have multiple command lines.
- Persistence: All
tmux
sessions can be saved in-state as sessions. So if you're at an airport working on important files on a remote server and your flight is about to take off, you can save exactly where you were and come back to it once you've landed. - Shareable: You may share sessions, meaning if someone else is logged in with you, you can see what the other is typing. This can be useful for paired programming, which is a technique where one programmer suggests to the other what to do, while the other programmer runs the commands.
- The WOW factor: You'll look like a pro Linux user to any onlookers.
Installation
Before getting to the actual commands, let's install tmux
from either a remote server or your local computer.
If you're running Mac OS X, grab the Homebrew Package Mananger, and run a simple brew install
command.
$ brew install tmux
On Ubuntu, use apt-get
, and for CentOS, use yum
. For arch, use pacman
# Debian based (Ubuntu)
$ sudo apt-get install tmux
# CentOS or Fedora
$ sudo yum install tmux
# Arch
$ sudo pacman -S tmux
Getting Started with tmux
Now if you type tmux
on the command line, you should have a tmux session running, as indicated by a green status bar at the bottom of the window. Before we move on, print out any tmux cheatsheet and tape it to your wall next to the monitor.

tmux
separates each terminal interface into three categories: sessions, windows and panes. Panes are parts of a window, while windows make up a session. By separating multiple interfaces in three different layers, we're able to better organize our workspace.
tmux
comes with subcommands that are bound to certain keys. Below is the help page, where you can see the list of special keys.

Before typing in any special key, you must input the prefix. By default, tmux
's prefix is Ctrl-b. So for example, if we want to split a window horizontally, we type <Ctrl-b>, then %. There is a way in which we may change the prefix, which we'll discuss later in our configurations lesson. But for now, let's move onto how we can manage panes!
Pane Management: Ex-Commandstmux
Recall from the diagram above that panes are the lowest level of interface. Let's learn how to manage these panes.
1) Splitting a Window into Panes
You can split a window either horizontally or vertically to create two panes:
<Ctrl-b> "
<Ctrl-b> %
As practice, try obtaining the following panes:

Moving from one pane to the next is easy - simply use:
<Ctrl-b> [Up/Down/Right/Left]
You'll notice that if there doesn't exist a pane in an inputted direction, the tmux won't navigate at all.
Great! Now try navigating back to the first pane and splitting it horizontally.

<Ctrl-b> ;
Lastly, you may move to the current pane on the left and right with the below keystrokes:
<Ctrl-b> {
<Ctrl-b> }
The above keystrokes seem buggy if you're using Mac OS X, so it may be best to stick with the arrow keys. Try moving along all of your panes for practice.
3) Resizing Panes
To resize a particular pane, navigate to the one you'd like that pane and type:
<Ctrl-b> <Ctrl-[Up/Down/Left/Right]>
Try changing your pane sizes to look like this:

4) Rotating Panes
In case you don't like the positions of all panes, you may rotate them.
<Ctrl-b> <Ctrl-o>
5) Show Pane Numbers
Sometimes you'll need to show pane numbers to switch among panes.
<Ctrl-b> q

To close a pane type:
<Ctrl-b> x
You'll be prompted with a warning before the closing occurs.
Another way to close the window is to simply type exit
at the specific pane's command line.
Ex-Commands
Every command that we've inputted so far are simply shortcuts to actual text commands. If we want to input commands instead of using keys, we may enable ex-mode by typing:
<Ctrl-b> :
For example, if you're on a Mac OS X, the keys used to resize panes may be bound to another feature. No worries! We can still resize panes using ex-commands. Type in your control key, followed by a colon to activate ex-mode:

- -D 10
- Resizes pane down 10 cells.
- -U 10
- Resize pane upward 10 cells.
- -L 10
- Resize pane left 10 cells.
- -R 10
- Resize pane right 10 cells.
- -t 2 30
- Resize pane of id 2 down 30 cells.
Now try resizing the panes from the "Resizing Panes" section above. There are a ton of other ex-commands all listed in tmux
's man
page.
Great! By now you should be a tmux
pane manager expert. Let's move onto managing windows.
Window Management tmux
Not only can you create new panes, but you may also start entirely new windows. This is useful, especially when you're moving onto another part of the project and need more space.
When you start tmux with the tmux
command, you're automatically put into a new window. At the bottom of the status bar, you can see the window's ID, along with its name. The asterisk (*) indicates the current window you're in.

Here are just 7 commands you can use to manage your windows.
1) Creating a New Window
The following keystroke will create a new window:
<Ctrl-b> c
To create two new windows, one with name "work", and the other named "playground," type:
$ tmux new-window -n work
$ tmux new-window -n playground

As mentioned above, the bottom green row, shows all your current open windows. The asterisk * indicates which window you are current working in. To switch to the next or previous windows use:
<Ctrl-b> n
<Ctrl-b> p
Another way to switch windows is through the window number.
<Ctrl-b> <0-9>
If you're more inclined to just use the command line, you may do so with:
$ tmux next-window
$ tmux previous-window
$ tmux last-window
3) Listing Windows
To list windows and select one to switch to:
<Ctrl-b> w
You may also just view which windows are available by typing:
$ tmux list-windows
0: bash- (2 panes) [81x28] [layout 18c6,81x28,0,0[81x11,0,0,0,81x16,0,12,1]] @0
1: bash* (1 panes) [81x28] [layout c2e0,81x28,0,0,3] @2 (active)

To rename a window, type the following into the terminal of a window you wish to rename:
$ tmux rename-window play
You may also just type:
<Ctrl-b> ,
5) Searching Windows
If you have too many windows open and can't find the one you need, you may use the following command to open up the find-window feature:
<Ctrl-b> f

6) Closing Windows
To close a window, use:
<Ctrl-b> &
To kill the current window:
& tmux kill-window
There is just one more level above windows - Sessions - which we'll cover next!
Session Management tmux
Sessions are used to separate out completed unrelated workspaces. One of the sessions started can be for actual company work, while another session can be for our own personal side project. We can keep track of these sessions by names, and close (dettach) and open (reattach) them with their current states saved.
7 Commands for Session Managemt
1) Creating a new Session
The moment you start Tmux with the tmux
command, you're placed in a session. If you'd like to name the session, use the new
subcommand:
$ tmux new -s work
Here, we named our session "work". We can also use the keystroke:
2) Renaming a session
To rename our session to "play", simply by using the rename-session
subcommand:
$ tmux rename-session play
Or use the keystroke:
<Ctrl-b> ,
3) Show all sessions
To get a listing of all sessions, type Ctrl-b s or type:
$ tmux list-sessions # Same as: $ tmux ls
work: 2 windows (created Tue Dec 22 14:00:52 2015) [85x24]
4) Detaching
Detaching means to save the session, unlike just exiting, which doesn't save. This feature is one of the hallmarks of tmux, and is what makes it persistent. To detach the current session, use the keystroke:
<Ctrl-b> d
Or the command:
$ tmux-detach
5) Attaching
Attaching a session is used to return back to a previous session that you detached from. To re-attach a session, use:
$ tmux attach -t work
The name of the session can be found from the list-sessions
subcommand as shown above.
6) Switching
To switch to a particular session, use either the keystroke:
$ tmux switch -t session_name
7) Destroying
To get rid of a session, type:
<Ctrl-b> d
Sharing Sessions
Probably the coolest feature of sessions is that you can share it with other users, much like a Google Docs file. This means that if two users are SSH'ed into a remote host, and are using the same session, they can see exactly what the other is typing in real-time.

$ tmux list-sessions
base: 1 windows (created Tue Dec 22 13:04:34 2015) [80x24] (attached)
$ tmux attach -t base
You should now have control of both windows! This won't be of any use alone, so go make some friends and perhaps you guys can have a fun paired-programming session!
Customizing and Installing Plugins tmux
Now that we have a good understanding of how to manage sessions, windows and panes, let's learn how to customize tmux
.
Always use tmux!
A good habit to form is to always have tmux
running whenever you open a shell. We can have tmux run upon startup by adding this snippet into our ~/.bash_profile
if [[ ! $TERM =~ screen ]]; then
tmux attach -t base || tmux new -s base
fi
Editing Your Configuration File
To set tmux
's system-wide settings on a Linux system, edit the /etc/tmux.conf file. For more user-specific settings, edit ~/.tmux.conf.
1) Reloading the Config File
To reload the configuration file, type:
$ tmux source-file ~/.tmux.conf
You can also bind the r
key to easily reload as a keystroke. Place the following in your ~/.tmux.conf file:
bind r source-file ~/.tmux.conf
2) Changing the prefix key
One of the first things users can do is to change the prefix key from <Ctrl-b> to <Ctrl-a>. Now that we know how to rebind the prefix key, we'll refer to your binding as prefix instead of Ctrl-b.
# Sets the prefix to Ctrl-a
set -g prefix C-a
# Releases binding from Ctrl-b
unbind C-b
bind C-a send-prefix
The -g indicates a global preference, meaning it applies all around tmux
.
3) Pane Switching
It could be cumbersome having to use the prefix key every time you want to switch panes.
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D
The M is the meta key (oftentimes the alt-key). Now you can simply hold onto alt+[Up/Down/Left/Right] to move among panes.
4) Enabling Mouse Mode
Sometimes you'll miss those GUI mouse-activated controls. We can re-active them using these three mouse modes:
# Enable mouse control
set -g mouse-select-pane on
set -g mouse-resize-pane on
set -g mouse-select-window on
Now you can:
- Click into a pane to activate it.
- Resize a pen by clicking and dragging the borders.
- Select a window by clicking on the tab.
5) Status Line
You can also configure the status line to select what it should display, and whether it should be on or off.
You can set the date with variables from strftime. For example, %c will display the date and time.
set -g status-right "Code Snipcademy %c"
set -g status-bg white
set -g status-fg black

You can also use the following variable keys for a more dynamic status bar.
- #H
- Hostname of local host.
- #S
- Session name.
- #T
- Current window title.
- ##
- Literal #.
- #(command)
- First line of command's output.
6) Intuitive Pane Splitting
If you're anything like myself, you may find it difficult to remember that % is for splitting horizontally and " is for splitting vertically. We can map the commands to - and | instead.
unbind %
unbind '"'
bind | split-window -h
bind - split-window -v
Installing Plugins
Plugins are a fast and easy way to obtain features without writing tens of lines of code. In fact, most of what we configured above are covered in some plugin. Let's learn how to install the tmux-pain-control plugin as an example. Feel free to browse through the list of tmux plugins for more plugin ideas.
Plugin Manager
To get started, download the tmux Plugin Manager (tpm):
$ git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
You will now be set to install any plugins.
Installing Your First Plugin
Now open ~/.tmux.conf with Vim:
set -g @tpm_plugins ' \
tmux-plugins/tpm \
tmux-plugins/tmux-pain-control \
'
run-shell '~/.tmux/plugins/tpm/tpm'
To install the plugin, place your cursor over the plugin, then press prefix I. You should get a window pop up like this (except with install statuses):

- move along our panes with prefix [h/j/k/l], which is similar to Vim controls.
- split panes with - and |.
- resize panes with prefix [H/J/K/L]
Remember that if there's a good nifty shortcut or trick, there's most likely a plugin for it. If you have a good idea for customization, try first looking for a plugin - it'll make your life easier!
What's your favorite configuration or plugin? Share in the comments below!