tmux: A terminal multiplexer for daily use

A terminal multiplexer is a software designed to multiplex multiple virtual terminal sessions inside a single terminal window. Wikipedia describes it pretty accurate:

A terminal multiplexer is a software application that can be used to multiplex several virtual consoles, allowing a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session. It is useful for dealing with multiple programs from a command line interface, and for separating programs from the Unix shell that started the program.

You might already know GNU screen, the most well known terminal multiplexer in the UNIX world.

What is tmux?

tmux is a terminal multiplexer and alternative to GNU screen.

Screen was born in 1987 and there were a lot of fixes and enhancements, which led to a (sometimes) complicated and obscure code base.

Apparently one guy named Nicholas Marriott wasn’t happy about the code base of screen, and he began the development of tmux in 2007. Today tmux has a solid and extensible code base, and it’s maintained by several contributors on github.

How do I get tmux?

There are several options to get tmux.

You can download a release on github and compile it all yourself. This is the way to go if you like to have the latest version of tmux on your fingertips.

But there are also pre-compiled versions of tmux available on most current Linux distributions. I’m a Debian guy, so here’s how you install tmux on your Debian-based Linux distribution:

apt-get update
apt-get install tmux

You can do the same on Ubuntu.

Right now I don’t have a RHEL, Fedora or CentOS at my disposal, but IMHO there’s no official package available for RHEL and CentOS – and I’m not even surprised by that ;). Though there’s an official Fedora package available. And for those using RHEL and CentOS, there are some manuals available, how you install tmux on RHEL & CentOS – or of course you switch to a proper Linux distribution 😉

How do I use tmux?

Well, this is quite a complicated topic, because tmux has so many features. I can’t really talk about all features, because it would be too complex – and to be honest, I don’t know them all!

Sessions

Let’s start with the most important thing, how to open a new tmux session:

# open an unnamed tmux session
tmux

# open a named tmux session
tmux new-session -s foobar

If you’ve disconnected from your tmux session, you can attach your existing session again:

# List sessions
tmux list-sessions

# Attach to unnamed session
tmux attach-session

# Attach to named session
tmux attach-session -s foobar

Because tmux is pretty smart, you can even shorten your commands. For example instead of attach-session, you can simply type att or even a is enough.

Status bar

As soon as you’ve started tmux, you should see a status bar on the bottom of your terminal. The status bar will show you the session name (or number), and the windows you’ve opened inside tmux. The current window is suffixed with an asterisk *, and the last window with a dash .

Key bindings

When you’re inside a tmux session, you can start to benefit from tmux, by using key bindings to do awesome things. By default, all key bindings are prefixed by hitting Ctrl-B. So for example, if you want to create a new window, hit Ctrl-B c.

Windows

Tmux is based on windows. For simplicity’s sake, let’s say one window = one terminal. This isn’t completely true, but let’s say it is for now.

Here are some shortcuts for window management. Remember, you’ve to hit Ctrl-B first!

  • c creates a new window
  • x kills the current window
  • , renames the current window
  • n next window
  • p previous window
  • f find window with specific string
  • 0…9 jump to window 0…9

I could add several more shortcuts here, but I think these are the most important ones.

Panes

Now it gets a bit more complicated, because there are panes as well. We just dealt with windows so far, and I told you one window is one terminal. This isn’t completely true, because each window can hold several panes.

With panes you can split windows, so you can see multiple terminals in one single window / screen.

Let’s start with some basic key bindings:

  • % splits the window vertically
  •  splits the window horizontally
  • o loops through panes
  • cursor keys focus another pane (left/right/down/up)
  • z zooms the pane

When you’ve opened several panes, you can easily swap them:

  • { moves pane to left
  • } moves pane to right
  • space loops through layouts

To resize panes, it gets more complicated:

Hit the prefix (by default Ctrl-B) then :. Now you’re in command mode, and you can enter one of the following commands:

resize-pane -D <cells>
resize-pane -U <cells>
resize-pane -L <cells>
resize-pane -R <cells>

This will resize the current pane Down, Up, Left or Right.

Configuration

Of course tmux is configurable by a config file, and you can configure nearly everything how you like it. By default, tmux will read the configuration file only when it wasn’t already running. Tmux tries to load the configuration files from /etc/tmux.conf, then ~/.tmux.conf.

There are bazillion of configuration parameters. Here are some important ones:

When you’re configuring tmux, you don’t want to kill your whole tmux sessions just to reload the config. So add the following lines to your config:

unbind r
bind r source-file ~/.tmux.conf\; display-message "Config reloaded..."

Now you’ve to restart tmux or hit Ctrl-B : reload-config ~/.tmux.conf at least once. From now on, you can edit your config and hit Ctrl-B r  to reload it.

tmux has also built-in mouse support, so let’s enable that:

setw -g mode-mouse on
set -g mouse-resize-pane on
set -g mouse-select-pane on
set -g mouse-select-window on
set -g mouse-utf8 off

Try to select a pane or window by clicking with the mouse on it. You should also be able to resize panes by drag n drop. Of course you need a terminal with built-in mouse support.

Also enable vi-style key bindings:

set -g status-keys vi
set -g mode-keys vi

I do also like to hit Ctrl-B Ctrl-B to jump between to the last window:

unbind C-b
bind C-b last-window

And because I’m lazy, I sometimes forget to release the Ctrl key:

unbind C-c
bind C-c new-window

unbind C-d
bind C-d detach

unbind C-x
bind C-x kill-pane

unbind x
bind x kill-pane

If you want to customise your status line, you can do that as well 🙂

# left and right status line
set -g status-left "#S"
set -g status-right "%a, %d.%b %H:%M:%S"

# set status format for windows
setw -g window-status-format         "#[fg=black,bg=cyan] #I #[fg=white,bg=black] #W "
setw -g window-status-current-format "#[fg=black,bg=cyan] #I #[fg=cyan,bg=white] #W "

# set default status line colour
set -g status-fg white
set -g status-bg black

# set default pane colour
set -g pane-border-fg black
set -g pane-active-border-fg cyan

# let window blink on activity, bell or content-change
setw -g window-status-activity-attr blink
setw -g window-status-bell-attr blink
setw -g window-status-content-attr blink

# message style
set -g display-time 1000
setw -g message-fg white
setw -g message-bg red

# update status every second
set -g status-interval 1

# monitor activity
setw -g monitor-activity on
set -g visual-activity on

# unicode
setw -g utf8 on
set -g status-utf8 on

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.