Fundamentals 8 min read

Understanding tmux: Concepts, Usage, and Custom Scripts

This article explains what tmux is, describes its client‑server architecture, sessions, windows and panes, and shows how to use copy‑mode, scripting with split‑window and send‑keys, custom configuration, and troubleshooting tips for effective terminal multiplexing.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Understanding tmux: Concepts, Usage, and Custom Scripts

tmux is a terminal multiplexer that allows multiple windows and split panes within a single terminal session, enabling users to run several tasks side by side.

It follows a client‑server model: the first tmux invocation starts a server and a client, creating sessions and windows; subsequent invocations start only a client that connects to the existing server, which manages all sessions.

Example of listing sessions:

ivan@louyi-MacBook:~$ tmux list-sessions
0: 2 windows (created Wed May 22 00:41:59 2013) [87x31] (attached)
2: 1 windows (created Wed May 22 00:51:42 2013) [204x43]

Each session can contain multiple windows (similar to browser tabs), and each window can be divided into multiple panes (like split‑screen tabs).

Practical usage includes keeping SSH connections alive across terminal closures by running the first SSH session inside tmux, so that detaching and re‑attaching does not disrupt the connection.

tmux provides a copy‑mode (enterable with C‑b [ ) that lets users scroll back through history, set a vi‑style key table, select text with v , copy with y , and paste from the buffer using C‑b ] or list buffers with C‑b # .

Because tmux is easily scriptable, you can automate pane creation and command execution. Example function to create a new window for logs:

function split_window {
  win="${prefix}_log"
  tmux new-window -t $session: -n $win
}

Further scripting shows how to split panes proportionally:

for i in $(seq 1 $((row_count - 1))); do
  row_pct=$(( (row_count - i) * 100 / (row_count - i + 1) ))
  tmux split-window -p $row_pct -v
 done

Conditional column splitting:

if [ "$col_count" -gt 1 ]; then
  # column split logic here
fi

More complex loops select panes and split them horizontally:

for i in $(seq 1 $row_count); do
  tmux select-pane -t $((session + (i - 1) * $col_count))
  for j in $(seq 1 $((col_count - 1))); do
    col_pct=$(( (col_count - j) * 100 / (col_count - j + 1) ))
    tmux split-window -p $col_pct -h
  done
 done

Functions to send commands to specific panes and to parse host patterns are also provided, enabling batch SSH and log‑tailing across multiple servers.

A complete log‑viewing script (multi‑win‑tmux.sh) demonstrates how to invoke the above functions, passing a host pattern and a tail command, with example usage:

$multi-win-tmux.sh l-review[1-3].ugc.cn6 "tail -f /home/q/www/review.qunar.com/logs/catalina.out"

The author’s tmux configuration sets vi mode for copy‑mode and changes the default prefix key:

setw -g mode-keys vi
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection
unbind C-b
set -g prefix "C-o"

A known issue is that when a pane receives garbled output (e.g., raw memcache data), the entire window can become unreadable, requiring the user to kill the window with C‑b & .

For additional commands and detailed options, consult the tmux manual.

session managementshell scriptingcopy modeterminal multiplexerTmux
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.