- 技術 💻
tmuxの使い方|キーバインド早見表とコピペ用.tmux.conf
tmuxとは、1つの端末の中で複数の画面(セッション・ウィンドウ・ペイン)を切り替えながら使え、SSHが切れても作業をサーバー側に残しておける「端末多重化ツール(ターミナルマルチプレクサ)」です。 画面を分割してエディタとログを同時に見る、detach で作業を置いたまま帰って翌日 attach で続きをする、といった使い方が基本になります。
この記事では、既定の prefix(C-b)で使うキーと、それに対応するコマンド名を1つの早見表にまとめたうえで、tmux 3.4 で実際に new -d → split-window → send-keys → capture-pane -p を実行して取った分割画面の実出力、コピペ用の .tmux.conf と各行の意味の表、開発環境を1コマンドで立ち上げるシェルスクリプト、よくある困りごとの実際のエラー文と対処表を載せています。最新の安定版は tmux 3.7c(2026年8月17日、3.7のバグ修正版。公式 GitHub Releases で確認)で、本文の検証はこのコンテナに入っている tmux 3.4 で行っています(キーバインドと設定の書き方は 3.2 以降で共通です)。
tmuxの早見表(キーバインドと対応コマンド)
キーは「prefix(既定は Ctrl+b)を押して離してから、次のキーを押す」という操作です。表のキー列は prefix のあとに押すキー、コマンド列は prefix : のコマンドプロンプトやシェルの tmux ... で使える名前です(tmux 3.4 の tmux list-keys から抜粋)。
セッション(作業のまとまり。detach しても残る)
| やりたいこと | キー(prefix のあと) | コマンド |
|---|---|---|
| 新しいセッションを作る | (シェルから) | tmux new -s 名前 |
| セッション一覧を見る | (シェルから) | tmux ls |
| セッションに接続する | (シェルから) | tmux attach -t 名前 |
| 無ければ作り、あれば接続する | (シェルから) | tmux new -A -s 名前 |
| 切断する(作業は残る) | d |
detach-client |
| 一覧から選んで切り替える | s |
choose-tree -s |
| 前 / 次のセッションへ | ( / ) |
switch-client -p / -n |
| セッション名を変える | $ |
rename-session 新名 |
| セッションを終了する | (シェルから) | tmux kill-session -t 名前 |
| サーバーごと全部終了する | (シェルから) | tmux kill-server |
ウィンドウ(タブに相当)
| やりたいこと | キー | コマンド |
|---|---|---|
| 新しいウィンドウを作る | c |
new-window -n 名前 |
| 次 / 前のウィンドウへ | n / p |
next-window / previous-window |
| 番号で移動する | 0〜9 |
select-window -t :番号 |
| 直前のウィンドウへ戻る | l |
last-window |
| 一覧から選ぶ | w |
choose-tree -w |
| 名前を変える | , |
rename-window 名前 |
| 名前で検索する | f |
find-window 文字列 |
| 隣と入れ替える | — | swap-window -t -1 / +1 |
| 閉じる(確認あり) | & |
kill-window |
ペイン(1つのウィンドウを分割した区画)
| やりたいこと | キー | コマンド |
|---|---|---|
| 左右に分割する | % |
split-window -h |
| 上下に分割する | " |
split-window -v |
| 上下左右へ移動する | ↑ ↓ ← → |
select-pane -U/-D/-L/-R |
| 次のペインへ | o |
select-pane -t :.+ |
| 直前のペインへ戻る | ; |
last-pane |
| 番号を表示して選ぶ | q のあと数字 |
display-panes |
| サイズを1ずつ / 5ずつ変える | Ctrl+矢印 / Alt+矢印 |
resize-pane -U/-D/-L/-R [数] |
| 最大化 / 元に戻す(ズーム) | z |
resize-pane -Z |
| 上 / 下のペインと入れ替える | { / } |
swap-pane -U / -D |
| レイアウトを順に切り替える | Space |
next-layout |
| 定型レイアウトにする | Alt+1〜Alt+5 |
select-layout even-horizontal など |
| ペインを別ウィンドウにする | ! |
break-pane |
| 別ウィンドウのペインを持ってくる | — | join-pane -s 元 -t 先 |
| 全ペインに同時入力する | — | setw synchronize-panes on |
| 閉じる(確認あり) | x |
kill-pane |
コピーモードとその他
| やりたいこと | キー | コマンド |
|---|---|---|
| コピーモードに入る(スクロール) | [ |
copy-mode |
| 1ページ上から入る | PageUp |
copy-mode -u |
| 貼り付ける | ] |
paste-buffer |
| バッファ一覧から選んで貼る | = |
choose-buffer |
| 最新バッファの中身を見る | — | show-buffer |
| キー一覧を見る | ? |
list-keys -N |
| コマンドプロンプトを開く | : |
command-prompt |
| アプリに prefix そのものを送る | Ctrl+b(2回押し) |
send-prefix |
| メッセージ履歴を見る | ~ |
show-messages |
| 設定を読み直す | — | source-file ~/.tmux.conf |
| 設定値を確認する | — | show-options -g / -s / -gw |
本記事の内容は、ご自由にお使いください。
ご利用の際は、出典として本ページへのリンクを記載いただけますようお願いします。
(記載例)出典:株式会社RJC「tmuxの使い方|キーバインド早見表とコピペ用.tmux.conf」
tmuxの仕組み:サーバー・セッション・ウィンドウ・ペイン
tmux は「サーバー」という常駐プロセスが画面の中身を持っていて、端末(クライアント)はそれに接続して見ているだけ、という構造です。だから端末を閉じても(SSH が切れても)サーバーの中のシェルは動き続け、attach で戻れます。
flowchart TB
C1["クライアント(手元の端末A)"] -->|attach| S["tmux サーバー(1ユーザーに1つ)"]
C2["クライアント(手元の端末B)"] -->|attach| S
S --> S1["セッション myapp"]
S --> S2["セッション ops"]
S1 --> W0["ウィンドウ 0: edit"]
S1 --> W1["ウィンドウ 1: server"]
W0 --> P0["ペイン 0: vim"]
W0 --> P1["ペイン 1: シェル"]
tmux サーバー(ソケット /tmp/tmux-UID/default)
└─ セッション "myapp" ← detach / attach の単位。tmux ls に出る
├─ ウィンドウ 0 "edit" ← タブ。ステータス行に 0:edit と並ぶ
│ ├─ ペイン 0 (%0) ← 画面の区画。1つずつシェルが動く
│ └─ ペイン 1 (%1)
└─ ウィンドウ 1 "server"
└─ ペイン 0 (%2)
| 用語 | 何か | 指定のしかた(-t) |
覚え方 |
|---|---|---|---|
| サーバー | 全セッションを持つ常駐プロセス | -L 名前 / -S パス で別サーバー |
1ユーザー1つが基本 |
| セッション | 作業のまとまり | -t myapp |
プロジェクト単位で1つ |
| ウィンドウ | セッション内のタブ | -t myapp:1 または -t myapp:server |
役割(編集・サーバー・ログ)単位 |
| ペイン | ウィンドウ内の区画 | -t myapp:edit.1(番号)または -t %1(ID) |
番号は並び順で変わり、ID(%1)は不変 |
ペインの「番号」は位置で振り直されますが、「ID(%0, %1)」は作成時のまま変わりません。 スクリプトで特定のペインを狙うときは ID を使うと確実です(後述の dev.sh で使っています)。
インストールは Ubuntu / Debian なら sudo apt install tmux、macOS なら brew install tmux、Fedora 系なら sudo dnf install tmux、Windows は WSL 上の Linux に入れます。tmux -V でバージョンを確認でき、この記事の検証環境では tmux 3.4 と表示されました。最新版は公式の GitHub Releases(tmux/tmux)で確認でき、2026年10月時点の最新安定版は 3.7c(2026年8月17日)、3.6 系は 2025 年末〜2026 年前半、3.5 系は 2024 年秋に出ています。
まず動かす:セッションを作って分割して、画面を取り出す(実行結果つき)
最初の一歩は tmux new -s 名前 で作り、prefix % で左右に、prefix " で上下に分割する、だけです。ここでは同じことを、画面を見なくても追えるようにコマンド版で実行し、capture-pane -p でペインの中身を取り出しています(tmux 3.4、ウィンドウサイズ 80×24。検証環境では見やすいようにプロンプトを $ にしています)。
tmux new -d -s demo -n editor -x 80 -y 24 # -d: 接続せずに作る(画面なしでも動く)
tmux split-window -h -t demo:editor # 左右に分割(prefix % と同じ)
tmux split-window -v -t demo:editor.1 # 右側(ペイン1)を上下に分割(prefix " と同じ)
tmux send-keys -t demo:editor.0 'echo hello from pane 0' Enter # ペインにキー入力を送る
tmux send-keys -t demo:editor.1 'echo hello from pane 1' Enter
tmux send-keys -t demo:editor.2 'echo hello from pane 2' Enter
tmux ls
tmux list-windows -t demo
tmux list-panes -t demo:editor
tmux capture-pane -p -t demo:editor.2 # ペインの画面内容を標準出力へ
実行結果(空行は省略):
$ tmux ls
demo: 1 windows (created Thu Oct 1 12:38:00 2026)
$ tmux list-windows -t demo
0: editor* (3 panes) [80x24] [layout d67e,80x24,0,0{40x24,0,0,0,39x24,41,0[39x12,41,0,1,39x11,41,13,2]}] @0 (active)
$ tmux list-panes -t demo:editor
0: [40x24] [history 0/2000, 2299 bytes] %0
1: [39x12] [history 0/2000, 1804 bytes] %1
2: [39x11] [history 0/2000, 1764 bytes] %2 (active)
$ tmux capture-pane -p -t demo:editor.2
$ echo hello from pane 2
hello from pane 2
$
同じ状態を端末で attach すると、こう見えます(80×24 の疑似端末で attach し、端末エミュレータライブラリ pyte で描画したもの。細線がペインの境界、最下行がステータス行です):
$ echo hello from pane 0 │$ echo hello from pane 1
hello from pane 0 │hello from pane 1
$ │$
│
│
├───────────────────────────────────────
│$ echo hello from pane 2
│hello from pane 2
│$
│
[demo] 0:editor* "vm" 12:38 01-Oct-26
| ステータス行の表示 | 意味 |
|---|---|
[demo] |
セッション名 |
0:editor* |
ウィンドウ番号:名前。* は今見ているウィンドウ、- は直前のウィンドウ |
"vm" |
ペインのタイトル(既定はホスト名) |
12:38 01-Oct-26 |
時刻と日付(status-right で変更可) |
続けて tmux new-window -t demo -n server でウィンドウを足し、tmux send-keys -t demo:server 'python3 -m http.server 7710' Enter のようにウィンドウ名で指定してコマンドを投入できます。tmux kill-session -t demo で最後のセッションを消すとサーバーも終了し、tmux ls は no server running on /tmp/tmux-<UID>/default と表示します。これは異常ではなく「何も動いていない」という意味です。
SSH切断からの復帰:detach と attach
detach(prefix d)は「端末だけ離す」操作で、セッションの中のプロセスはそのまま動き続けます。 SSH が切れたときも同じで、サーバー側の tmux セッションは残ります。戻るときは tmux attach -t 名前 です。
tmux new -d -s dev # 作る
tmux attach -t dev # 接続する(ここで作業)
# ... prefix d で切断、または SSH が切れる ...
tmux ls # 残っていることを確認
tmux attach -t dev # 続きから
attach 中と detach 後の違いを、別の端末から tmux ls で見た実出力です(prefix d はクライアント側で押しています):
### attach 中(別の端末から見る)
$ tmux ls
dev: 1 windows (created Thu Oct 1 12:42:16 2026) (attached)
$ tmux list-clients
/dev/pts/1: dev [80x24 xterm-256color] (attached,focused,UTF-8)
### クライアント側で prefix d を押す
[detached (from session dev)]
### detach 後
$ tmux ls
dev: 1 windows (created Thu Oct 1 12:42:16 2026)
| 状況 | コマンド | ポイント |
|---|---|---|
| 名前が分からない | tmux ls → tmux attach -t 名前 |
(attached) が付いていれば別端末で接続中 |
| 無ければ作り、あれば接続(ログイン直後に便利) | tmux new -A -s dev |
二重作成の duplicate session が起きない |
| 別端末で開いたままの画面が小さい | tmux attach -d -t dev |
-d で他のクライアントを切断してから接続 |
| 既に tmux の中にいる | tmux switch-client -t dev |
中から attach すると入れ子になる |
| 切断して、同時にセッションも終える | exit(各シェルで)か tmux kill-session -t dev |
detach は終了ではない |
セッション名はプロジェクト名(order-api)や役割(ops、logs)で付けると tmux ls で迷いません。 .(ドット)と :(コロン)は -t の区切り文字と衝突するので名前に使わないのがコツです。サーバーの再起動で tmux のセッション自体は消えるため、「再起動後も復元したい」場合は後述の tmux-resurrect / tmux-continuum を使います。
コピー&ペースト:コピーモードとクリップボード連携の考え方
prefix [ でコピーモードに入り、選択してコピーすると tmux の「バッファ」に入り、prefix ] で貼り付けます。 既定のキー体系は emacs 風で、.tmux.conf に setw -g mode-keys vi と書くと vi 風になります。
| 操作 | emacs(既定) | vi(mode-keys vi) |
|---|---|---|
| 選択開始 | Ctrl+Space |
Space(本記事の設定では v も) |
| 選択をコピーして終了 | Alt+w |
Enter(本記事の設定では y も) |
| 行全体を選択 / 矩形選択の切り替え | — / R |
V / v(既定。本記事の設定では v を選択開始に変更) |
| 下 / 上へ検索、次 / 前の一致 | Ctrl+s / Ctrl+r、n / N |
/ / ?、n / N |
| 行頭 / 行末、履歴の先頭 / 末尾 | Ctrl+a / Ctrl+e、Alt+< / Alt+> |
0 / $、g / G |
| 1ページ上 / 下 | PageUp / PageDown |
Ctrl+b / Ctrl+f |
| 終了 | q または Esc |
q |
vi 設定で prefix [ → 0 → v → $ → y の順にキーを押したあと、バッファを確認した実出力です:
$ tmux show-buffer
copy-me-please
$ tmux list-buffers
buffer0: 15 bytes: "copy-me-please\n"
tmux のバッファは tmux の中だけのものなので、OS のクリップボードに渡すには「橋渡し」が必要です。 考え方は3通りあります。
| 方法 | 仕組み | 向く場面 | 設定の要点 |
|---|---|---|---|
| copy-pipe で外部コマンドへ渡す | コピー時に xclip / wl-copy / pbcopy / clip.exe を実行 |
ローカルの端末で使う | copy-pipe-and-cancel "xclip -selection clipboard -i" |
| OSC 52 で端末に渡す | tmux がエスケープシーケンスで端末エミュレータに送る | SSH 越し(リモートの tmux から手元のクリップボードへ) | set -s set-clipboard on + 端末側が OSC 52 対応 |
| 端末側の選択を使う | tmux を通さず端末が文字を選ぶ | とにかく今すぐ | mouse on のときは Shift を押しながらドラッグ |
copy-pipe の行は次の .tmux.conf にコメントで入れてあるので、自分の環境の1行だけ有効にしてください。xclip が無い環境で tmux-yank 等を入れると「tmux-yank dependencies not installed!」と表示されるため、まず which xclip wl-copy pbcopy で手元のコマンドを確認します。
コピペ用 .tmux.conf(各行の意味つき)
次の設定を ~/.tmux.conf に保存し、tmux source-file ~/.tmux.conf(または設定後は prefix r)で反映します。 tmux 3.4 で読み込みと各キーの動作を確認済みで、3.2 以降で同じ書き方が使えます(3.2 未満では terminal-features の行を set -as terminal-overrides ",xterm-256color:Tc" に置き換えます)。
# ~/.tmux.conf コピペ用(tmux 3.4 で動作確認。3.2 以降で使える書き方)
# ---- 1. prefix を C-b から C-a へ ----
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# ---- 2. 設定の再読み込み(prefix r)----
bind r source-file ~/.tmux.conf \; display-message "tmux.conf reloaded"
# ---- 3. マウス(クリックでペイン選択、ドラッグで境界移動、ホイールでスクロール)----
set -g mouse on
# ---- 4. 色:256色 + true color ----
set -g default-terminal "tmux-256color"
set -as terminal-features ",xterm-256color:RGB"
# ---- 5. 履歴行数(既定 2000)とEscの遅延(既定 500ms)----
set -g history-limit 50000
set -sg escape-time 10
# ---- 6. 番号を1始まりにし、閉じたら詰める ----
set -g base-index 1
setw -g pane-base-index 1
set -g renumber-windows on
# ---- 7. 分割と新規ウィンドウは今のディレクトリを引き継ぐ ----
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
bind c new-window -c "#{pane_current_path}"
# ---- 8. ペイン移動は h/j/k/l、サイズ変更は H/J/K/L(-r で連打できる)----
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5
# ---- 9. vi 風コピーモード(v で選択開始、y でコピー)----
setw -g mode-keys vi
bind -T copy-mode-vi v send-keys -X begin-selection
bind -T copy-mode-vi y send-keys -X copy-selection-and-cancel
# OSなどのクリップボードへも渡すときは、下のどれか1行を有効にして上の y 行を置き換える
# bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard -i" # Linux(X11)
# bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "wl-copy" # Linux(Wayland)
# bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "pbcopy" # macOS
# bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "clip.exe" # WSL
set -s set-clipboard on
# ---- 10. ステータス行 ----
set -g status-position top
set -g status-interval 5
set -g status-style "bg=colour236,fg=colour250"
set -g status-left " #S | "
set -g status-left-length 20
set -g status-right " #h | %Y-%m-%d %H:%M "
setw -g window-status-format " #I:#W "
setw -g window-status-current-format " #I:#W "
setw -g window-status-current-style "bg=colour33,fg=colour255,bold"
# ---- 11. その他 ----
set -g display-time 2000
set -g focus-events on
各行の意味
| 行 | 意味 | 既定値 | 補足 |
|---|---|---|---|
set -g prefix C-a / unbind C-b |
prefix を Ctrl+a に変更 | C-b |
Ctrl+a は bash の「行頭へ」なので、bind C-a send-prefix で2回押しすると素通しできる |
bind r source-file ... \; display-message |
prefix r で設定を読み直して通知 |
— | \; で複数コマンドをつなぐ |
set -g mouse on |
マウスでペイン選択・境界ドラッグ・ホイールスクロール | off |
端末側の文字選択は Shift+ドラッグ |
set -g default-terminal "tmux-256color" |
ペイン内の $TERM |
環境により screen か tmux-256color |
この環境の 3.4 では既定が tmux-256color だった |
set -as terminal-features ",xterm-256color:RGB" |
外側の端末が true color 対応だと tmux に伝える | — | -a は追記。3.2 未満は terminal-overrides ... :Tc |
set -g history-limit 50000 |
スクロールバック行数 | 2000 |
新しく作るペインから有効 |
set -sg escape-time 10 |
Esc 単独と Alt+キーを区別する待ち時間(ms) | 500 |
vim で Esc が遅く感じるときの定番 |
base-index 1 / pane-base-index 1 / renumber-windows on |
番号を1から振り、閉じたら詰める | 0 / 0 / off |
キーボードの並びに合う。スクリプトで .0 を指定していると can't find pane: 0 になる点に注意 |
bind | split-window -h -c "#{pane_current_path}" / bind - ...-v |
prefix | で左右、prefix - で上下分割。今のディレクトリを引き継ぐ |
% / " |
-c が無いとセッション作成時のディレクトリで開く。既定キーも残る |
bind h/j/k/l select-pane / bind -r H/J/K/L resize-pane |
vim と同じ向きで移動、Shift 付きで 5 ずつサイズ変更 | 矢印 / Ctrl+矢印 |
-r は repeat-time(既定 500ms)内なら prefix なしで連打可。l は既定の last-window を上書き |
setw -g mode-keys vi / bind -T copy-mode-vi v / y |
コピーモードを vi 風にし、v で選択開始・y でコピー終了 |
emacs / Space / Enter |
既定の v は矩形切替。クリップボードに渡すなら copy-pipe-and-cancel "コマンド" |
set -s set-clipboard on |
OSC 52 で端末のクリップボードにも送る | external |
端末が未対応でも害はない |
status-position top / status-left / status-right |
ステータス行を上に、左にセッション名、右にホスト名と日時 | bottom / [#S] / ペインタイトルと日時 |
#S セッション名、#h ホスト名、#I ウィンドウ番号、#W ウィンドウ名。strftime 形式が使える |
window-status-current-style "bg=colour33,..." |
今のウィンドウの色 | — | colour0〜colour255、または #RRGGBB |
display-time 2000 / focus-events on |
メッセージ表示時間(ms) / フォーカスイベントをアプリに渡す | 750 / off |
vim の autoread などが反応する |
読み込みと動作の確認(実行結果)
設定が効いているかは show-options と list-keys で確認できます。 検証では ~/.tmux.conf を書き換えずに tmux -f 設定ファイル で起動し、実際のキー入力(クライアント経由)で分割・リロード・ズームを押して確かめました。
tmux -f ~/.tmux.conf new -d -s conf -n main -x 80 -y 24 # エラーが出なければ構文は通っている
tmux show-options -g | grep -E '^(prefix|mouse|history-limit|base-index|status-position|renumber-windows) '
tmux show-options -s escape-time
tmux show-options -gw mode-keys
base-index 1
history-limit 50000
mouse on
prefix C-a
renumber-windows on
status-position top
escape-time 10
mode-keys vi
Ctrl+a | → Ctrl+a - → 各ペインで echo → Ctrl+a h で左へ移動、と押したあとの画面(番号が 1 始まり、ステータス行が上):
conf | 1:main vm | 2026-10-01 12:39
$ echo pane 1 │$
pane 1 │
$ │
│
├───────────────────────────────────────
│$ echo pane 3
│pane 3
│$
Ctrl+a r を押すと、ステータス行の位置に tmux.conf reloaded が 2 秒表示されました。Ctrl+a z でズームすると #{window_zoomed_flag} が 1、もう一度押すと 0 に戻ります。
true color の設定が効いたかは、attach 中に #{client_termfeatures} を見ると分かります。 terminal-features の行がある場合と無い場合の実出力です(外側の端末は TERM=xterm-256color):
# 設定なし
$ tmux display -p '#{client_termname} #{client_termfeatures}'
xterm-256color bpaste,ccolour,clipboard,cstyle,focus,title
$ tmux info | grep -E '^ *[0-9]+: setrgbf'
210: setrgbf: [missing]
# set -as terminal-features ",xterm-256color:RGB" あり
$ tmux display -p '#{client_termname} #{client_termfeatures}'
xterm-256color bpaste,ccolour,clipboard,cstyle,focus,RGB,title
$ tmux info | grep -E '^ *[0-9]+: setrgbf'
210: setrgbf: (string) \033[38;2;%p1%d;%p2%d;%p3%dm
setrgbf に 24bit 色のシーケンスが入れば、ペインの中で printf '\e[38;2;255;100;0mTrue Color\e[0m\n' のような出力がそのままの色で表示されます。設定ファイルに誤りがあると読み込み時に行ごとのエラーが出ます(mouse を mosue と書いた例: invalid option: mosue)。
開発環境を1コマンドで立ち上げるスクリプト(実行確認済み)
毎回「分割してサーバーを起動してログを開いて」と手で作る代わりに、tmux のコマンドをシェルスクリプトに並べておけば ./dev.sh の1回で同じ配置が再現できます。 下のスクリプトは「編集用(左右分割)」「開発サーバー」「ログ監視」の3ウィンドウを作って接続します。既にセッションがあれば作らずに接続するので、二重起動しません。
#!/usr/bin/env bash
# dev.sh : 開発環境を1コマンドで立ち上げる(tmux 3.4 で動作確認)
# ウィンドウ 0 "edit" : 左=エディタ用シェル / 右=作業用シェル
# ウィンドウ 1 "server" : 開発サーバー、ウィンドウ 2 "logs" : ログを tail -f
set -eu
SESSION="myapp" # セッション名(プロジェクト名にすると ls で見分けやすい)
ROOT="${1:-$PWD}" # 第1引数にプロジェクトのパス(省略時はカレント)
SERVER_CMD="python3 -m http.server 7711 --bind 127.0.0.1"
LOG_FILE="$ROOT/app.log"
# 既にあれば作らずに接続する(二重起動の防止)
if tmux has-session -t "$SESSION" 2>/dev/null; then
echo "session '$SESSION' already exists -> attach"
exec tmux attach -t "$SESSION"
fi
touch "$LOG_FILE"
# 1) セッションとウィンドウ0を detached で作る(-c で作業ディレクトリ、-x/-y は初期サイズ)
tmux new-session -d -s "$SESSION" -n edit -c "$ROOT" -x 120 -y 36
# 2) ウィンドウ0を左右に分割。ペインは「番号」でなく「ID(%0 など)」で指定すると
# base-index の設定に左右されない(-P -F '#{pane_id}' で新ペインの ID を受け取る)
LEFT=$(tmux display-message -p -t "$SESSION:edit" '#{pane_id}')
RIGHT=$(tmux split-window -h -t "$SESSION:edit" -c "$ROOT" -P -F '#{pane_id}')
tmux send-keys -t "$LEFT" 'echo "[edit] open your editor here, e.g. vim ."' Enter
tmux send-keys -t "$RIGHT" 'git status 2>/dev/null || echo "[shell] ready"' Enter
# 3) ウィンドウ1でサーバー起動、4) ウィンドウ2でログ監視(ウィンドウは名前で指定できる)
tmux new-window -t "$SESSION" -n server -c "$ROOT"
tmux send-keys -t "$SESSION:server" "$SERVER_CMD" Enter
tmux new-window -t "$SESSION" -n logs -c "$ROOT"
tmux send-keys -t "$SESSION:logs" "tail -f '$LOG_FILE'" Enter
# 5) 最初に表示するウィンドウとペインを選び、接続
tmux select-window -t "$SESSION:edit"
tmux select-pane -t "$LEFT"
if [ -n "${TMUX:-}" ]; then
tmux switch-client -t "$SESSION" # すでに tmux の中なら切り替え
else
tmux attach -t "$SESSION"
fi
実行結果(端末を持たない検証環境から実行したため、最後の attach だけ「not a terminal」で終わります。手元の端末から実行すればそのまま接続されます):
$ bash dev.sh ./proj
open terminal failed: not a terminal
$ tmux ls
myapp: 3 windows (created Thu Oct 1 12:41:18 2026)
$ tmux list-windows -t myapp
0: edit* (2 panes) [120x36] [layout 56b4,120x36,0,0{60x36,0,0,0,59x36,61,0,1}] @0 (active)
1: server (1 panes) [120x36] [layout c0ff,120x36,0,0,2] @1
2: logs- (1 panes) [120x36] [layout c100,120x36,0,0,3] @2
$ tmux list-panes -s -t myapp -F '#{window_name}.#{pane_index} #{pane_current_command}'
edit.0 bash
edit.1 bash
server.0 python3
logs.0 tail
$ curl -s http://127.0.0.1:7711/ | head -1 # server ウィンドウのサーバーに届く
<!DOCTYPE HTML>
$ echo "hello log" >> proj/app.log ; tmux capture-pane -p -t myapp:logs | tail -2
$ tail -f '.../proj/app.log'
hello log
$ bash dev.sh ./proj # 2回目は作らずに attach へ
session 'myapp' already exists -> attach
attach したときの edit ウィンドウ(80×24 で描画):
[edit] open your editor here, e.g. vim .│[shell] ready
$ │$
│
[myapp] 0:edit* 1:server 2:logs- "vm" 12:41 01-Oct-26
スクリプトで気をつける点は「ペイン番号は設定で変わる」ことです。 最初このスクリプトを -t "$SESSION:edit.0" と番号で書いて base-index 1 の環境で動かすと、can't find pane: 0 で止まりました(実際の出力)。split-window -P -F '#{pane_id}' で受け取った ID(%1 など)を -t に使い、ウィンドウは -n で付けた名前で指定すれば、どの設定でも同じように動きます。send-keys の末尾の Enter を忘れると入力待ちのまま止まる点も、よくあるつまずきです。
プラグイン(TPM):セッション復元やクリップボード連携を足す
プラグインは TPM(Tmux Plugin Manager)で管理するのが一般的です。 TPM 本体を git clone し、.tmux.conf に @plugin を並べて prefix I(大文字のアイ)でインストール、prefix U で更新、prefix Alt+u で一覧から外したものを削除します。
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# ~/.tmux.conf の末尾に追記(run の行は必ず最後)
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible' # 無難な既定値(escape-time 0、history-limit 50000 など)
set -g @plugin 'tmux-plugins/tmux-resurrect' # prefix C-s で保存、prefix C-r で復元
set -g @plugin 'tmux-plugins/tmux-continuum' # 定期保存と起動時の自動復元
set -g @plugin 'tmux-plugins/tmux-yank' # コピーをOSのクリップボードへ(xclip 等が必要)
set -g @resurrect-capture-pane-contents 'on' # ペインの表示内容も保存
set -g @continuum-restore 'on' # tmux サーバー起動時に自動復元
run '~/.tmux/plugins/tpm/tpm'
この記事の検証コンテナでは GitHub からの取得ができたため、作業フォルダに TPM(2026年5月版)を clone し、bin/install_plugins で上の4つを入れて動作を確認しました(~/.tmux.conf ではない設定ファイルで検証したため、プラグイン一覧は @tpm_plugins オプションで渡しています。通常の ~/.tmux.conf なら @plugin 行で動きます):
$ ~/.tmux/plugins/tpm/bin/install_plugins
Already installed "tpm"
Installing "tmux-sensible"
"tmux-sensible" download success
Installing "tmux-resurrect"
"tmux-resurrect" download success
...(continuum, yank も同様に download success)
$ tmux list-keys -T prefix | grep resurrect
bind-key -T prefix C-r run-shell .../plugins/tmux-resurrect/scripts/restore.sh
bind-key -T prefix C-s run-shell .../plugins/tmux-resurrect/scripts/save.sh
$ tmux show -s escape-time ; tmux show -g history-limit # tmux-sensible が変更
escape-time 0
history-limit 50000
prefix Ctrl+s を押すと ~/.local/share/tmux/resurrect/ にセッション・ウィンドウ・ペイン(作業ディレクトリ、実行中コマンド)がテキストで保存されました。なお xclip などが無い環境では tmux-yank のキーが「Error! tmux-yank dependencies not installed!」というメッセージに置き換わるため、ローカル端末で使うときに入れるのが向いています。
よくある困りごとと対処表
つまずきの多くは「設定が反映されていない」「端末の情報が足りない」「tmux の中で tmux を開いている」のどれかです。 実際に出たエラー文で引けるようにしました。
| 症状・エラー文 | 原因 | 対処 |
|---|---|---|
| prefix を押しても何も起きない | .tmux.conf を変えたが読み直していない、別サーバーに接続している、tmux の中の tmux に送っている |
tmux show -g prefix で現在値を確認 → tmux source-file ~/.tmux.conf。入れ子なら prefix を2回押すと内側へ届く |
sessions should be nested with care, unset $TMUX to force |
tmux のペインの中で tmux new / attach を実行した |
中からは tmux switch-client -t 名前。作るだけなら tmux new -d -s 名前。本当に入れ子にするなら TMUX= tmux attach -t 名前 |
| 色が 8 色 / 16 色しか出ない、Vim の配色が崩れる | ペインの $TERM が screen、または true color が伝わっていない |
set -g default-terminal "tmux-256color" と set -as terminal-features ",xterm-256color:RGB"。確認は tput colors(256)と #{client_termfeatures} に RGB |
| Vim で Esc を押してからノーマルモードになるまで一瞬待つ | escape-time の既定 500ms |
set -sg escape-time 10 |
open terminal failed: not a terminal |
cron・CI・パイプなど端末の無い場所から attach / new(-d なし)を実行 |
作るだけなら tmux new -d、コマンド投入は send-keys、結果は capture-pane -p で取る |
open terminal failed: terminal does not support clear |
$TERM が dumb など、terminfo に機能が無い |
TERM=xterm-256color tmux attach のように端末種別を指定 |
no server running on /tmp/tmux-1000/default / no sessions |
セッションを1つも作っていない、またはサーバー再起動で消えた | 正常な状態。tmux new -s 名前 で作る。再起動後も戻したいなら resurrect / continuum |
duplicate session: dev |
同名セッションがある状態で new -s dev |
tmux new -A -s dev(あれば attach)か tmux attach -t dev |
can't find pane: 0 / can't find window: logs |
base-index / pane-base-index が 1、またはウィンドウ名の打ち間違い |
番号でなくペイン ID(%1)とウィンドウ名で指定する |
invalid option: mosue |
設定ファイルのタイプミス | エラー行の名前を直して source-file |
| マウスで文字を選択・コピーできない | mouse on だと tmux がドラッグを受け取る |
Shift(macOS の一部端末は Option)を押しながらドラッグ、または prefix [ のコピーモード |
画面の端に . が並んで小さい |
別のクライアントが小さい端末で attach している | tmux attach -d -t 名前 で他を切断、または set -g window-size latest |
| 日本語が崩れる | 端末のロケールが UTF-8 でない | LANG=ja_JP.UTF-8(または C.UTF-8)を設定、tmux -u で UTF-8 を強制 |
よくある質問
Q. screen と tmux はどちらを使えばよいですか?
A. どちらも端末多重化ツールで、detach / attach の考え方は同じです。tmux はペイン分割・レイアウト・send-keys などのスクリプト化・プラグインが充実しており、新しく覚えるなら tmux を選ぶ人が多い状況です。screen の Ctrl+a に慣れているなら、この記事の設定で prefix を C-a に変えると違和感が小さくなります。
Q. .tmux.conf を変えたのに反映されません。
A. 設定ファイルはサーバー起動時にしか自動では読まれません。tmux source-file ~/.tmux.conf を実行するか、この記事の設定なら prefix r を押します。history-limit や default-terminal のように「新しく作るペインから効く」ものもあるので、効かないときは新しいウィンドウを開いて確かめます。
Q. ペインに入力したコマンドの結果をファイルに残したいです。
A. tmux capture-pane -p -S - -t 対象 > out.txt で履歴の先頭(-S -)から全部を書き出せます。長時間の記録なら tmux pipe-pane -o -t 対象 'cat >> ~/pane.log' でペインの出力をそのままファイルに流し続けられます。
Q. 複数のサーバーに同じコマンドを打ちたいです。
A. 各サーバーへの SSH を1つのウィンドウのペインに並べ、setw synchronize-panes on を有効にすると、入力が全ペインに同時に送られます。終わったら off に戻します。状態をステータス行に出すには #{?pane_synchronized,SYNC,} を status-right に入れます。
まとめ
- tmux は「サーバーが画面を持ち、端末は見ているだけ」の構造なので、detach や SSH 切断後も
tmux attach -t 名前で続きから作業できる - 階層はセッション → ウィンドウ → ペイン。指定は
-t セッション:ウィンドウ.ペイン、スクリプトではペイン ID(%1)とウィンドウ名を使うと設定に左右されない - 基本キーは prefix(C-b)のあとに
cnp(ウィンドウ)、%"矢印z(ペイン)、d(切断)、[](コピーと貼り付け) .tmux.confは prefix 変更・マウス・true color・escape-time・vi コピーモード・ステータス行の順に入れ、prefix rで読み直す。効いたかはshow-optionsとlist-keysで確認- 開発環境は
new -d→split-window -P -F '#{pane_id}'→send-keysをスクリプトにして1コマンド化。has-sessionで二重起動を防ぐ - クリップボード連携は copy-pipe(ローカル)か OSC 52(SSH 越し)。再起動後の復元は tmux-resurrect / continuum
- エラー文は「not a terminal」「nested with care」「no server running」「duplicate session」「can't find pane」を押さえれば大半は解決できる
本記事の内容は、ご自由にお使いください。
ご利用の際は、出典として本ページへのリンクを記載いただけますようお願いします。
(記載例)出典:株式会社RJC「tmuxの使い方|キーバインド早見表とコピペ用.tmux.conf」
株式会社RJCでは、tmux のような日々の開発環境を整える道具の使い方も、プロジェクトの中で仲間と共有しながら磨いています。こうした知識を共有しながら、日々学び合っています。
株式会社RJC ― SI事業・SES事業・AI駆動開発。RJCは一緒に成長を楽しめる会社です。
WE ARE HIRING
RJCで一緒に開発しながら、
成長を楽しみませんか?
RJCは、Web・モバイル・AIを活用した開発プロジェクトで、テックリードやPM・PMOも活躍するシステム開発会社です。会社を知る、待遇を確かめる、話を聞いてみる。気になるところ見てみてください!
- 127日年間休日
- 12時間平均残業時間
- 毎日ガチャ遊びココロも大切にする福利厚生。アマギフなどの賞品ラインナップ!
ほかにも、チケットレストラン、書籍読み放題、2年ごとの慰労報奨(休暇 or 金一封)、11期連続の黒字決算。