How to use tmux, a cheat sheet

tmux-linux

I think we know that screen is officially old news and most modern Linux systems switched to tmux as the recommended multiplexer with RHEL 8. Recently I had a discussion amongst my DBA gang for tmux migration and decided to write this. If you’re still using screen, now’s the time to upgrade: tmux is fun! (I know I’m late in the party but better late than never)

This tmux cheat sheet combins session management, logging, reloading config, and scrolling commands in a quick reference format. You can also configure the tmux commands identical to screen by config file (using Ctrl-a).

Tmux Demo Session

Tmux Session Management

ActionCommand / Shortcut
Start new sessiontmux new -s session_name
List sessionstmux ls
Attach sessiontmux attach -t session_name
tmux a (last session)
Detach sessionCtrl + b then d
Kill sessiontmux kill-session -t session_name
(or connect session and exit)
Attach a sessiontmux attach-session -t session_name
Forcefully take-over a sessiontmux attach-session -d -t session_name

Tmux Logging

ActionCommand
Start logging current panetmux pipe-pane -o 'cat >> ~/tmux.log'
Stop loggingtmux pipe-pane

Tmux Reload Config

ActionCommand / Shortcut
Reload configtmux source-file ~/.tmux.conf (from shell)
Reload config (inside tmux)Ctrl + b then : then source-file ~/.tmux.conf

Tmux Scrolling / Copy Mode

ActionCommand / Shortcut
Enter copy/scroll modeCtrl + b then [
Scroll up/downArrow keys, Page Up / Page Down
Exit copy modeq or Esc
Enable mouse scrollingAdd “set -g mouse on” to ~/.tmux.conf and reload

Tmux Key Bindings

ActionShortcut
Detach sessionCtrl + b then d
List sessionsCtrl + b then s
Rename sessionCtrl + b then $
New windowCtrl + b then c
Split horizontallyCtrl + b then "
Split verticallyCtrl + b then %
Navigate panesCtrl + b then Arrow keys


Configuration customization

You can customize using tmux.conf configuration file further with additional bind keys for your workflow!

If you’re habituated to screen command you can change the default shortcut command from “Ctrl-b” to “Ctrl-a” using configuration. There’s more power in the configuration, have a look.

tmux.conf: Key Settings & Bindings

ActionCommand / Config Line
Enable default pane logging to timestamped fileset-option -g default-command ‘tmux pipe-pane -o “cat >>~/tmxlogs/tmx-#{session_name}-\`date +%Y%m%dT%H%M%S\`.log”; /bin/bash -l’
Enable mouse supportset -g mouse on
Increase scrollback/history buffer sizeset -g history-limit 10000
Reload tmux config shortcut (prefix + r)bind r source-file ~/.tmux.conf \; display-message "Config reloaded!"
Unbind default prefix key (Ctrl+b)unbind C-b
Set prefix key to Ctrl+a (Screen style)set-option -g prefix C-a
Reload config with prefix + Shift + Rbind-key R source-file ~/.tmux.conf \; display-message "Config reloaded!"
Sample ~/.tmux.conf

You can use the following configuration:

# vim ~/.tmux.conf

# Enable default logging
set-option -g default-command 'tmux pipe-pane -o "cat >>~/tmxlogs/tmx-#{session_name}-\`date +%Y%m%dT%H%M%S\`.log"; /bin/bash -l'

# Enable mouse support for scrolling and pane selection
set -g mouse on

# Increase scrollback / log history buffer size
set -g history-limit 10000

# Reload config shortcut - Ctrl+b, R to reload config changes
bind r source-file ~/.tmux.conf \; display-message "Config reloaded!"

# Screen compatible commands settings
unbind C-b # Unbind default prefix
set-option -g prefix C-a # Set prefix to Ctrl+a (same as screen)

To make use of configuration changes for the first time you need to run

tmux source ~/.tmux.conf

Once you have added “Reload config shortcut” you just need to press: Ctrl-a, r to reload the changes.

Exit mobile version