2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cmux で Markdown プレビューを複数タブにまとめる

2
Last updated at Posted at 2026-06-03

複数の Markdown ファイルをプレビューしたいとき、開くたびにペインが増えて他の作業スペースが狭くなった経験はないでしょうか。

cmux markdown open をそのまま使うと、呼び出すたびに新しいペインが右に追加され続けます。

# 3ファイル開いた後
[terminal] | [f1.md] | [f2.md] | [f3.md]
              ↑ ペインがどんどん増えて全部狭くなる

この記事では、cmux の markdown openopen コマンドを組み合わせて、右半分の1ペインにタブとして集約する方法を紹介します。

ゴール

[ターミナル(左半分)] | [preview: file1.md] [preview: file2.md] ...
                                  ↑ タブで切り替え

複数のプロセスから独立して呼んでも、プレビューは同じペインに積み上がります。

使うコマンド

1枚目:右に分割して開く

cmux markdown open file.md --direction right

--direction right で現在のペインを左右に分割し、右半分に Markdown プレビューを開きます。

2枚目以降:既存ペインにタブ追加

cmux open file2.md --surface <surface_id> --no-focus

cmux open は複数ファイルを同じペインにタブとして開けます。--surface で追加先のペインを指定し、--no-focus でフォーカスをターミナル側に保ちます。

surface ID の取得

cmux tree --workspace <workspace_id> | grep '\[markdown\]' | grep -o 'surface:[0-9]*'

ラッパースクリプト

上記の判定をまとめたスクリプトです。PATH の通った場所に置いてください。

cmux-md
#!/bin/bash
set -euo pipefail

# workspace 自動検出
workspace=$(cmux identify --no-caller 2>/dev/null | grep -o 'workspace:[0-9]*' || true)
if [[ -z "$workspace" ]]; then
  workspace=$(cmux tree 2>/dev/null | grep '◀ here' | head -1 | grep -o 'workspace:[0-9]*' || true)
fi
[[ -z "$workspace" ]] && echo "Error: not in a cmux workspace" >&2 && exit 1

# --close: 全タブを閉じる
if [[ "$1" == "--close" ]]; then
  surfaces=$(cmux tree --workspace "$workspace" 2>/dev/null | grep '\[markdown\]' | grep -o 'surface:[0-9]*' || true)
  for s in $surfaces; do
    cmux close-surface --surface "$s" --workspace "$workspace"
  done
  exit 0
fi

# 絶対パス変換
file="$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
[[ ! -f "$file" ]] && echo "Error: file not found" >&2 && exit 1

# 既存の markdown ペインがあればタブ追加、なければ新規作成
md_surface=$(cmux tree --workspace "$workspace" 2>/dev/null | grep '\[markdown\]' | grep -o 'surface:[0-9]*' | head -1 || true)

if [[ -n "$md_surface" ]]; then
  cmux open "$file" --workspace "$workspace" --surface "$md_surface" --no-focus
else
  cmux markdown open "$file" --workspace "$workspace"
fi

使い方

# 1枚目(右半分にペインが作られる)
cmux-md README.md

# 2枚目以降(同じペインにタブが増える)
cmux-md CHANGELOG.md

# 全タブを閉じる
cmux-md --close

複数のプロセスから独立して呼んでも、右半分のペインが共有されるので、作業スペースがすっきり保たれます。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?