Table of Contents

tmux is a terminal multiplexer which means that you can have multiple windows open in a single terminal, it also supports the feature of sessions (akin to screen) meaning you can SSH into a remote machine, start a session under tmux, log out and then connect to your previous session with continuity, very handy.

Session Management

A simple table for reference of the commonly used commands for managing sessions.

Command Action
tmux list-sessions List existing sessions.
tmux new-session -s mongo Start a new session with name mongo.
tmux attach -t default Attach to session with the name default.

A useful trick for servers where you might like to always connect to a tmux session is to add the following to the end of your ~/.bashrc which will attach you to the default tmux session if one exists, if not it will start one with that name.

if [ -z "$TMUX" ]; then
    tmux attach -t default || tmux new -s default
fi

Actions and Navigation

Tmux works rather like Emacs' key bindings, but instead of C-u or M-x that you would use under Emacs you to initiate all key sequences C-b (of which there are many, many, many to learn). Common commands can have custom values set or redefined entirely should you desire.

Action Type Command Action
Sessions C-b d Detach from current session.
C-b $ Rename session.
C-b Reload configuration.
Scrolling C-b [PgUp|PgDn] Scroll up/down the buffer (q to exit and return to command prompt).
Panes C-b % also C-b V Split pane vertically.
C-b " also C-b H Split pane vertically.
C-b x Kill a pane.
Windows C-b c Create a new window.
C-b , Rename current window.
C-b & Close current window.
C-b n Next window.
C-b p Previous window.
Panes C-b : setw synchronize-panes on Sends the same commands you type to every pane/window, very handy if e.g. updating multiple systems.

Mouse

If you've used GNU/Linux desktop environments for any period of time you'll likely find the mouse an invaluable tool for copying and pasting between things, highlight something then use the third mouse button to paste it in the target. To continue using this feature under tmux you have add the following line to your config and use the Shift button in conjunction with your highlighting and pasting.

set -g mouse on

Configuration

Tmux is highly configurable and combined with powerline (a Python module originally for Vim but which supports Bash and Zsh) allows highly informative (and much more importantly pretty) decoration around the command line prompt and window in general. All configuration options go in the ~/.tmux_conf file but it is perfectly permissible for this file to source others.

Tmux on SSH

Since the ability to connect/disconnect from sessions is one of the major appeals of Tmux I wanted to ensure that whenever I SSH'd into my server I connected to the same Tmux session, or if none had been started one was created. The solution was to add the following to .bashrc although I ensured that I used $SSH_TTY as explained in one of the answers to ensure that I didn't screw up sftp/rsync connections made over SSH.

snippet.bash
if [[ -z "$TMUX" ]] && [ "$SSH_TTY" != "" ]; then
    tmux attach-session -t ssh_tmux || tmux new-session -s ssh_tmux
fi

Sharing Tmux Sessions

Start a Tmux session with an explicit socket, change the group ownership of it to a group that all potential users are members of they can then specify that socket and attach to the session.

snippet.bash
tmux -S /tmp/shared-tmux new -s shared-session
chgrp users /tmp/shared-tmux

Other users who are members of users can now attach to the session with…

snippet.bash
tmux -S /tmp/shared-tmux attach -t shared-session

Links

Custom Configuration

Themes

Plugins

HowTos