15
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

なんとなく、Emacs で Claude Code 周りをうまく使えないかなと思い、パッケージを見ていたら良さそうなものを見つけたので、そのパッケージを紹介します。

Emacs と Claude Code

Emacs で Claude Code を利用するためのパッケージはいくつか存在します。

今回は、Anthropic 公式が提供しているIDE の統合と同等の機能に対応しているという点で、claude-code-ide.el を紹介します。(Emacs が IDE であるかどうかはさておき…)

インストール手順

Emacs を使ったことがない方でも設定できるよう、一から環境を整えて説明します。

ただし、Emacs 自体のインストールについては、別途調べてください。
また、Emacs 28.1 以上のバージョンがインストールされている必要があるので、注意してください。

パッケージマネージャーのインストールと設定

Emacs のパッケージマネージャーはいくつかありますが、ここでは Elpaca を利用します。

ドキュメントにある Installation に従って、.emacs.d/init.el ファイルに以下の設定を記述します。

.emacs.d/init.el
;;; init.el --- -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:

;; Install Elpaca
(defvar elpaca-installer-version 0.11)
(defvar elpaca-directory (expand-file-name "elpaca/" user-emacs-directory))
(defvar elpaca-builds-directory (expand-file-name "builds/" elpaca-directory))
(defvar elpaca-repos-directory (expand-file-name "repos/" elpaca-directory))
(defvar elpaca-order '(elpaca :repo "https://github.com/progfolio/elpaca.git"
                              :ref nil :depth 1 :inherit ignore
                              :files (:defaults "elpaca-test.el" (:exclude "extensions"))
                              :build (:not elpaca--activate-package)))
(let* ((repo  (expand-file-name "elpaca/" elpaca-repos-directory))
       (build (expand-file-name "elpaca/" elpaca-builds-directory))
       (order (cdr elpaca-order))
       (default-directory repo))
  (add-to-list 'load-path (if (file-exists-p build) build repo))
  (unless (file-exists-p repo)
    (make-directory repo t)
    (when (<= emacs-major-version 28) (require 'subr-x))
    (condition-case-unless-debug err
        (if-let* ((buffer (pop-to-buffer-same-window "*elpaca-bootstrap*"))
                  ((zerop (apply #'call-process `("git" nil ,buffer t "clone"
                                                  ,@(when-let* ((depth (plist-get order :depth)))
                                                      (list (format "--depth=%d" depth) "--no-single-branch"))
                                                  ,(plist-get order :repo) ,repo))))
                  ((zerop (call-process "git" nil buffer t "checkout"
                                        (or (plist-get order :ref) "--"))))
                  (emacs (concat invocation-directory invocation-name))
                  ((zerop (call-process emacs nil buffer nil "-Q" "-L" "." "--batch"
                                        "--eval" "(byte-recompile-directory \".\" 0 'force)")))
                  ((require 'elpaca))
                  ((elpaca-generate-autoloads "elpaca" repo)))
            (progn (message "%s" (buffer-string)) (kill-buffer buffer))
          (error "%s" (with-current-buffer buffer (buffer-string))))
      ((error) (warn "%s" err) (delete-directory repo 'recursive))))
  (unless (require 'elpaca-autoloads nil t)
    (require 'elpaca)
    (elpaca-generate-autoloads "elpaca" repo)
    (let ((load-source-file-function nil)) (load "./elpaca-autoloads"))))
(add-hook 'after-init-hook #'elpaca-process-queues)
(elpaca `(,@elpaca-order))

;;; init.el ends here

標準で入っているパッケージマネージャー package.el によるパッケージの読み込みを無効化するため、.emacs.d/early-init.el に以下の設定を記述します。

.emacs.d/early-init.el
;;; early-init.el --- -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:

;; Disable package loading via package.el
(setq package-enable-at-startup nil)

;;; early-init.el ends here

これで、Elpaca のインストールのための設定は完了しました。

次にパッケージのインストールに入りますが、その前にパッケージのインストールの記述を楽にするための設定を追記しておきます。
.emacs.d/init.dl に以下を追加します。

.emacs.d/init.el
;; Install use-package support
(elpaca elpaca-use-package
  ;; Enable use-package :ensure support for Elpaca.
  (elpaca-use-package-mode))

claude-code-ide.el のインストール

Elpaca の設定が完了したので、本命の claude-code-ide.el のインストールをしていきます。

claude-code-ide.elPrerequisites にある通り、Emacs のバージョンは 28.1 以上で、Claude Code CLI が既に導入されており、パスが通っている必要があります。
Claude Code CLI のインストールについても説明はしないので、適宜調べて設定してください。

また、vterm パッケージも必要です。
このパッケージの Requirements によると、Emacs のバージョン 25.1 以上、cmake のバージョン 3.11 以上、そして libtool-bin がインストールされていることが求められています。
そのため、cmakelibtool-bin のインストールを行います。

Debian 系のディストリビューションを使用している場合は、以下のコマンドでインストールできます。
その他の場合は、使用している環境に合わせてコマンドを置き換えてください。

$ sudo apt install cmake libtool-bin

上述の通り、cmake にはバージョンの要件があるので、念のためバージョンを確認します。

$ cmake --version
cmake version 3.28.3

私の環境では、3.28.3 だったので 3.11 以上という要件を満たしており、問題ありませんでした。

これで準備が整ったので、.emacs.d/init.el に追記してインストールを完了させます。

.emacs.d/init.el
;; Install flycheck
(use-package flycheck
  :ensure t
  :init (global-flycheck-mode))

;; Install Claude Code IDE for Emacs
(use-package claude-code-ide
  :ensure (:type git :host github :repo "manzaltu/claude-code-ide.el"))

flycheck をインストールすることで、エラーや警告といったコードの解析結果を表示できるようになります。
claude-code-ide.el は、このパッケージとの統合がされており、そのエラーや警告を Claude Code と共有することが可能です。

これで再度 Emacs を開くと、自動でパッケージのインストールが始まり、Claude Code が使用できる状態になります。

claude-code-ide.el の使い方

claude-code-ide.el を導入することで、Claude Code のドキュメントに記載されている、以下の機能と同等の機能を利用できます。

機能

  • クイック起動: Cmd+Esc(Mac)またはCtrl+Esc(Windows/Linux)を使用してエディターから直接Claude Codeを開くか、UIのClaude Codeボタンをクリックします
  • 差分表示: コードの変更をターミナルではなくIDE差分ビューアーで直接表示できます。これは/configで設定できます
  • 選択コンテキスト: IDEの現在の選択/タブがClaude Codeと自動的に共有されます
  • ファイル参照ショートカット: Cmd+Option+K(Mac)またはAlt+Ctrl+K(Linux/Windows)を使用してファイル参照(例:@File#L1-99)を挿入します
  • 診断共有: IDEからの診断エラー(lint、構文など)が作業中に自動的にClaudeと共有されます

Ref: IDEにClaude Codeを追加する

特別な設定をすることなく、これらの機能を利用することができます。

ただし、ショートカットに関しては、対応する Emacs 用のキーバインドの設定は用意されていません。
代わりに、いくつかのコマンドが用意されているので、それらと関連する部分に絞って説明します。

これらのコマンドは、キーバインドに割り当てて素早く呼び出せるようにしておくと便利です。

クイック起動

M-x の後に claude-code-ide と入力して Enter を押すと、以下のように Claude Code CLI が Emacs の画面内で起動します。

image.png
(一部の文字が豆腐になっていますが、私のフォント設定による問題なので無視してください。)

この時点で、現在 Emacs で開いているファイルや選択しているコードが Claude Code に認識されています。

ファイル参照ショートカット

M-x の後に claude-code-ide-insert-at-mentioned と入力して Enter を押すと、以下のように選択したファイルと行番号が挿入されます。

image.png

私の環境では、何故か一行ずれた値で挿入されてしまうようでしたが、現時点では原因を特定できていません。

おわりに

この記事では Emacs で Claude Code を利用するためのパッケージ claude-code-ide.el の導入方法と基本的な使い方を紹介しました。

Emacs に触れたことがない方も、ぜひこの機会に試してみてはいかがでしょうか。
新しい開発環境として、その魅力に気づくかもしれません。

全く関係ないですが、今年(2025年)で GNU Emacs のリリースから 40 年目1らしいです。

  1. GNU Emacs Release History

15
3
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
15
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?