0
2

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 コマンドをよく使う

0
Last updated at Posted at 2026-05-01

以前、cmux の組み込みブラウザで mo を開く zsh 関数 moo / moc を作ったという記事を書きました。

その後、「内容さえわかればいい」場面が増えてきて、いつのまにか cmux に内蔵の markdown コマンドをもっぱら使うようになっていました。

cmux markdown コマンド

cmux markdown <path>

指定したファイルを右隣のペインにレンダリングして表示します。見出し・コードブロック・テーブル・リストなどはひととおり対応しており、ライブリロードもあるのでファイルを編集するたびに自動で再描画されます。

方向を変えたいときは --direction で指定できます。

cmux markdown README.md --direction down

cmux を使っていれば追加インストール不要で、ペインが自然に分割されるのが気に入っています。「とにかく中身を確認したい」だけならこれで十分です。

cmux-md

自分の場合はドキュメントを書きながら内容をさっと確認するのによく使っています。一時的に開いて確認したら閉じる、という使い方なので viewer は常に1つだけ開く運用にしています。cmux markdown open の薄いラッパーとして cmux-md というスクリプトを作り、既存の viewer を閉じてから開き直す動作を一発にしています。

cmux-md <file.md>   # 既存 viewer を閉じて開き直す
cmux-md --close     # viewer を閉じる
cmux-md
#!/bin/bash
set -euo pipefail

usage() {
  echo "Usage: cmux-md [--workspace <ws>] <file.md>" >&2
  echo "       cmux-md [--workspace <ws>] --close" >&2
  echo "  Open a markdown file in cmux markdown viewer." >&2
  echo "  --workspace: specify workspace (e.g. workspace:1). Auto-detected if omitted." >&2
  echo "  --close: close all markdown viewers in current workspace." >&2
  exit 1
}

[[ $# -lt 1 ]] && usage

# --workspace オプションの処理
workspace=""
if [[ "$1" == "--workspace" ]]; then
  workspace="$2"
  shift 2
fi

[[ $# -lt 1 ]] && usage

# workspace 自動検出
if [[ -z "$workspace" ]]; then
  workspace=$(cmux identify --no-caller 2>/dev/null | grep -o 'workspace:[0-9]*' || true)
fi

# フォールバック: cmux tree から ◀ here を含む行の workspace を取得
if [[ -z "$workspace" ]]; then
  workspace=$(cmux tree 2>/dev/null | grep '◀ here' | head -1 | grep -o 'workspace:[0-9]*' || true)
fi

if [[ -z "$workspace" ]]; then
  echo "Error: not in a cmux workspace" >&2
  exit 1
fi

# --close: 全 markdown surface を閉じる
if [[ "$1" == "--close" ]]; then
  cmux tree --workspace "$workspace" 2>/dev/null | grep '\[markdown\]' | grep -o 'surface:[0-9]*' | while read -r s; do
    cmux close-surface --surface "$s" --workspace "$workspace"
  done
  exit 0
fi

file="$1"
[[ ! -f "$file" ]] && echo "Error: file '$file' not found" >&2 && exit 1

# 絶対パスに変換
file="$(cd "$(dirname "$file")" && pwd)/$(basename "$file")"

# 既存の markdown surface があれば閉じて新しく開く
existing=$(cmux tree --workspace "$workspace" 2>/dev/null | grep '\[markdown\]' | grep -o 'surface:[0-9]*' | head -1)

if [[ -n "$existing" ]]; then
  cmux close-surface --surface "$existing" --workspace "$workspace"
fi

cmux markdown open "$file" --workspace "$workspace"

refs

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?