👻

Claude CodeのセッションをMarkdownファイルにダンプするカスタムコマンドを書いた

に公開

https://gist.github.com/fumito-ito/77c308e0382e06a9c16b22619f8a2f83

最近Claude Codeをよく使っている。コードを生成する際のやり取りをコミットメッセージなどに含めたいことがよくあるのだが、そのようなコマンドが標準で用意されていなかった。

Claude Codeは簡単にカスタムスラッシュコマンドを作成することができるので、試しに作ったらそこそこうまく動いたのが冒頭のものである。

利用方法

Claude Codeを実行するプロジェクトに以下のようにmarkdownファイルを配置する。

mkdir .claude/commands
touch .claude/commands/dump.md # dump.mdの中身は冒頭のgistの内容とする

Claude Codeを起動し、いくつかのやりとりを行った後に /project:dump を実行すれば良い。

# 何らかのやりとり
> /project:dump

● I'll create a log file with the conversation data.

● Bash(mkdir -p .claude/logs)…
  ⎿  (No content)

● Bash(date "+%Y-%m-%d_%H-%M-%S")…
  ⎿  2025-03-18_05-21-58

● Create(file_path: .claude/logs/claude-code-session-2025-03-18_05-21-58.md)…
  ⎿  Updated .claude/logs/claude-code-session-2025-03-18_05-21-58.md with 39 additions
      1  # Session Date
      2  
      3  - start: 2025-03-18_05-20-00
      4  - dumped: 2025-03-18_05-21-58
      5  
      6  # Conversation
      7  
      8  ## 👤 User (2025-03-18_05-20-00)
      9  
     10  update
     11  
     12  ## 🤖 Claude (2025-03-18_05-20-05)
     13  
     14  On branch feature/dev-container
     15  Changes to be committed:
     16    (use "git restore --staged <file>..." to unstage)
     17    new file:   .devcontainer/devcontainer.json
     18    new file:   .vscode/settings.json
     19  
     20  Changes not staged for commit:
     21    (use "git add <file>..." to update what will be committed)
     22    (use "git restore <file>..." to discard changes in working directory)
     23    modified:   Sources/AnthropicSwiftSDK/Entity/Content/Content.swift
     24    modified:   Sources/AnthropicSwiftSDK/Entity/Content/DocumentContent.swift
     25    modified:   Tests/AnthropicSwiftSDKTests/Entity/ContentTests.swift
     26    modified:   Tests/AnthropicSwiftSDKTests/Entity/ModelTests.swift
     27  
     28  Untracked files:
     29    (use "git add <file>..." to include in what will be committed)
     30    .claude/
     31    .cursor/
     32  
     33  ## 👤 User (2025-03-18_05-21-55)
     34  
     35  cancel tasks
     36  
     37  ## 🤖 Claude (2025-03-18_05-21-56)
     38  
     39  Task canceled. Is there anything else you'd like me to help with?
     40 \ No newline at end of file

● The log file has been created at .claude/logs/claude-code-session-2025-03-18_05-21-58.md with the conversation data formatted as requested.

こんな感じでダンプが作成される。コミットメッセージにdumpの中身をピックアップして記入してもよし、誰かに使い方をレクチャーしたり、作業ログとして残してもよしという感じでなかなか気に入っている。

なお、プロジェクト毎ではなく毎回利用したい場合には、HOMEディレクトリにこのファイルを配置すれば良いようだ。

mkdir ~/.claude/commands
touch ~/.claude/commands/dump.md

# Claude Codeのセッション内
> /user:dump

参考リンク

https://docs.anthropic.com/en/docs/agents-and-tools/claude-code/tutorials#create-custom-slash-commands

Discussion