Tmux: Simple guide to The Terminal Multiplexer

10 Apr 2012

Tmux allows you to open and manage different virtual consoles. This is handy when you want to run a process or program that mustn't be interrupted because you lose connectivity with your server. It's also very handy when you are working on a script or configuration, as you can just leave the file opened in the console. I only started using Tmux about 6 months ago, but I already prefer it over GNU Screen.

Why TMUX

Tmux is easy to learn, easy to configure and easy to tweak. Some features:
  • Client/Server System When you launch a session, a server instance is created. Other sessions are then started as clients. (lightweight)
  • Synchronize-panes: You can issue one command that will be done across the four panes, each pane can have a different ssh session to another server (updating/upgrading on 4 different servers)
  • Multiplexing: one can have more than one window open
  • Persistence: sessions can be attached/detached. When a user detaches a session, processes in that session will keep running.
  • Shared Sessions: multiple users can use/view the same session

Installing tmux

To install tmux in debian/ubuntu:

apt-get install tmux

Or through yum repositories:

yum install tmux

To start tmux just type:

tmux

You should get something that looks like this:

Tmux commands

Time to play around with tmux commands:
  • Ctrl-b c Create new window
  • Ctrl-b d Detach current client
  • Ctrl-b l Move to previously selected window
  • Ctrl-b n Move to the next window
  • Ctrl-b p Move to the previous window
  • Ctrl-b & Kill the current window
  • Ctrl-b , Rename the current window
  • Ctrl-b % Split the current window into two panes
  • Ctrl-b q Show pane numbers (used to switch between panes)
  • Ctrl-b o Switch to the next pane
  • Ctrl-b ? List all keybindings
  • Ctrl-b n (Move to the next window)
  • Ctrl-b p (Move to the previous window)
  • Ctrl-b l (Move to the previously selected window)
  • Ctrl-b w (List all windows / window numbers)
  • Ctrl-b (Move to the specified window number, the default bindings are from 0 - 9)
  • Ctrl-b % (Split the window vertically)
  • Ctrl-b : “split-window” (Split window horizontally)
  • Ctrl-b o (Goto next pane)
  • Ctrl-b q (Show pane numbers, when the numbers show up type the key to goto that pane)
  • Ctrl-b { (Move the current pane left)
  • Ctrl-b } (Move the current pane right)

Using TMUX

First let's open some windows and panes, hit Ctrl-b c and then Ctrl-b , to rename the window (just think of a random name). To switch between these windows you can use Ctrl-b p and Ctrl-b n (next and previous window respectively). Then if we want to split this window into two panes, we use Ctrl-b %. To navigate between panes we can just use Ctrl-b "arrow key" or Ctrl-b q and then hit the number of the right pane. To automatically arrange the panes hit Ctrl-b "SPACEBAR".

Configuring Tmux

In your homefolder create a file called

.tmux.conf

. In here we can set multiple tmux parameters. For instance this my (edited) tmux configuration:

set-window-option -g utf8 on
set -g default-terminal screen-256color
set -g status-bg black
set -g status-fg white
set -g status-interval 1
set-option -g pane-active-border-fg green
set-option -g pane-active-border-bg black
set-option -g pane-border-fg white
set-option -g pane-border-bg black
set-window-option -g window-status-fg yellow
set-window-option -g window-status-bg black
set-window-option -g window-status-attr dim
set-window-option -g window-status-current-fg red
set-window-option -g window-status-current-bg default
set-window-option -g window-status-current-attr bright
set -g status-left '#[fg=green]#H#[fg=red]:#[fg=white]#S #[fg=green]][#[default]'
set -g status-right "#[fg=gray,bright]#(whoami)#[default,fg=white,bg=black]|#[fg=white,bg=black,bright]%a %d.%m.%Y %H:%M:%S#[default]"
set -g status-right-length 50

So what are we doing?

  • First Section
    • Enable UTF8
    • Enable 256 colors
    • Set the colors for the status messages
    • Set the time interval to refresh the status bar to one second
  • Second Section
    • Set some color options for the panes (the active pane has a green border, the inactive ones have a white border)
  • Third section
    • Set the window status colors (you can use dim and bright for contrast)

  • Fourth section
    • The first one sets some colors (keysign is #) and sets the left hand status to yourlocalhostname: windowsnames
    • The second line issues the command whoami and puts the ouput from this command in the right hand status together with the date.
    • The last line sets the length of the status to 50
Tmux variable expansion

(command) First line of command’s output

H Hostname of local host

S Session name

T Current window title

A literal ‘#’

Moving status to top

If you want to have the status bar on top of your screen, then you will have to compile the latest tmux yourself. You can check it out with svn here:

svn co https://tmux.svn.sourceforge.net/svnroot/tmux tmux

After installing it you should be able to use this option:

set-option -g status-position top

Synchronizing Panes

As I said you can enter a command in one pane, and it will be mirrored in all the other ones. To achieve this you need to hit Ctrl-b and type

:setw synchronize-panes

Now type something, you will see it appears on all the panes at the same time.

Server client model

Since there is one server and all the windows are a client for that session, we can move windows between sessions. To move a session:

tmux move-window -d -s [src-window] -t [dst-window]

The -d option means that any attached clients will become detached.

Sources