ZNHOO Whatever you are, be a good one!

Tmux

  1. Overview
  2. Configuration
  3. Try Tmux
  4. Tmux Sub-commands
  5. Tmux Panes Synchronization
  6. Tmux Command Key Bindings

Overview

Tmux mainly comprises the server daemon and client. The server daemon manages multiple sessions. When a session is created, the server daemon is lauchned automatically. The client displays a session by attach-session. The server and client are separate Tmux processes. They communicate through the socket under $TMPDIR, like "/tmp/tmux-xyz".

A session can create multiple windows like terminal TABs. Each window occupy the whole screen area and can further be splitted into multiple panes (rectangle area on screen). Each window or pane is a separate pseudo terminal.

We can regard Tmux Window and Pane as Emacs Frame and Window.

Configuration

Tmux supports separate configurations to server, session, window or pane by set-option (set).

  1. set -s configures the server.
  2. set configures a specific session.
  3. set -w (alias setw ) configures a specific window.
  4. set -p configures a specific pane. By default, pane inherits the configuration of window if there is not specific config.

Except the server, we can add extra -g to configures the default options. Usually, we add -g in ~/.tmux.conf. The following is a sample:

# /etc/tmux.conf
# ~/.tmux.conf

# copy mode
#set -gw mode-keys vi
set -gw mode-keys emacs

# scrollback
set -g history-limit 100000
set -g mouse on

# bring ssh-agent to tmux
set -g update-environment "SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION http_proxy HTTP_PROXY https_proxy HTTPS_PROXY ftp_proxy FTP_PROXY all_proxy ALL_PROXY no_proxy NO_PROXY"

To reflect the newest configuration file update, just source it.

Another interesting repo tmux-colors-solarized.

Try Tmux

Start new session:

~ $ tmux new-session -s session-name

To put Tmux background, we detach by Ctrl-b d shortcut.

List and kill session:

~$ tmux ls
~$ tmux kill-session -t session-name

Attatch to session:

# attach to latest detached session
~ $ tmux a | at | attatch

# attach to a speciic session
~ $ tmux a -t session-name

Tmux Sub-commands

Tmux has many sub-commands, like new-session, kill-session and attach-session above. Use tmux list-commands to show all supported sub-commands.

Many sub-commands has built-in aliases, like new for new-session. We can also just type a few leading characters of a sub-command as long as no ambiguity incurred. For example, attach-session can be expressed as just a single character a or a simple word att.

Once attached to a session, we can use key bindings to trigger sub-commands within the session. We call those key bindings command key bindings. Command key bindings share the same prefix key Ctrl-b. For example, to create a window for the session, use Ctrl-b c.

We can also manually input sub-commands within a session. Use Ctrl-b : to get into command mode, and then input the sub-command. For instance, Ctrl-b : source-file ~/.tmux.conf reloads Tmux configuration on the fly. If you mis-typed a sub-commands, then Ctrl-u and try again. To get out of command mode, just press ESC.

Tmux Panes Synchronization

Sometimes, we'd like to run the same commands in multiple servers. For such requirements, we have ansible/fabric/pssh etc. Alternatively, tmux support panes synchronization, namely synchronize the commands in one pane to others panes in the same window!

To make use this feature, just Ctrol-b : setw synchronize-panes on/off.

Tmux Command Key Bindings

Apart from command mode, Tmux has the copy mode by Ctrol-b [. Copy mode is used to navigate within shell screen in casef we don't have X (i.e. scroll up). Copy mode has its own key bingdings. By default, Tmux uses Emacs-like key bindings and gets out ESC. You can change to Vi style and get out by ENTER.

Check supported key bindings of copy mode by tmux list-keys -T copy-mode (without -T, list all Tmux key bindings). You will find that only part of Emacs or Vi key bindings are supported.

The following are some general Tmux key bingdings and the corresponding sub-commands.

Session operation:

: command mode
[ copy mode

s  list sessions
$  rename a session

Window operation:

c  create window
w  list windows
n  next window
p  previous window
f  find window
&  kill window

Pane operation:

%  vertical split
"  horizontal split

o  swap panes
x  kill pane
+  break pane into window (e.g. to select text by mouse to copy)
-  restore pane from window
⍽  space - toggle between layouts
q (pane numbers will show up on screen. When the numbers show up, press the relevant key, switching to that pane)
{ (Move the current pane left)
} (Move the current pane right)
z toggle pane zoom

Misc operation:

d  detach
t  big clock
?  list shortcuts