Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Understanding tmux: Concepts, Usage, and Custom Scripts
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.

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.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
Session Managementshell-scriptingcopy modeterminal multiplexertmux

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion