2
1

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を使う理由(6): AI

Last updated at Posted at 2025-12-21

はじめに

  • Emacsならでは、という使い方はしていないですね、、、:sweat_smile:
  • CLIの進化が激しくUIもよくできている(@/ コマンド)のと、EXWM経由なので快適です
  • 今は Claude Code君をメインとし、比較やレビュー用にgemini-cliCodexを使って、単純な作業は軽快な Qwen Code が、 いくらでも並列で並べられるのって、本当にAI革命は素晴しい です(ボトルネックは自分の頭)

Alacritty + tmux + ai-cli

  • 作業毎にtmuxのタブでcliを増やしていきます
  • 改行に迷う(Alt-Enter? Shift-Enter?)ことがあるので、Shift-Enterに統一してます
  • tmuxはChromeと同じようなCtrl-Tabのタブを使っていて、Emacsからも移動できるようにしていますが、あまり使ってない
  • 他、Emacsの選択範囲をCLIに送るような工夫をしていますが、ほぼ使ってない

Alacritty

.config/alacritty/alacritty.toml

[font]
size = 8

[colors.primary]
background = "#FFFFFF"
foreground = "#000000"

[font.normal]
family = "MeiryoKe_Console"

[selection]
# save_to_clipboard = true

bindings = [
  # tmuxのタブ操作に合せる
  { key = "Tab", mods = "Control", chars = "\u001b[27;5;9~" },
  { key = "Tab", mods = "Control|Shift", chars = "\u001b[27;6;9~" },
  
  # Ctrl-^, -でフォントサイズを変更
  { key = "^", mods = "Control", action = "IncreaseFontSize" },
  
  
  # Alt-wでコピー(ただしtmuxでは動作しない)
  { key = "W", mods = "Alt", action = "Copy" },
  
  # Win-yでペースト
  { key = "Y", mods = "Super", action = "Paste" },
  
  # 改行がAlt-EnterかShift-Enterで迷うことがあるので、Shift-Enterに統一
  { key = "Return", mods = "Shift", chars = "\u001b\r" }
]

tmux

.tmux.conf

# ステータスバーの色を灰色に設定
set -g status-style bg=colour252,fg=black

set -g base-index 1         # ウィンドウの番号を 1 から開始
setw -g pane-base-index 1   # ペインの番号を 1 から開始

set-option -g status-position top       # ステータスバーをトップに配置する
set -g status-interval 1                # ステータスバー更新速度 15s(default) → 1s

set-option -g status-right ""

bind r source-file ~/.tmux.conf \; display "Reloaded!"

set -g mouse on
# unbind -T copy-mode MouseDragEnd1Pane

bind-key -n C-Tab next-window
bind-key -n C-S-Tab previous-window
bind-key -n C-BTab previous-window

unbind C-b
set-option -g prefix C-t
bind C-t send-prefix

bind C-t new-window
bind C-o select-pane -t :.+

bind-key 2 split-window -v
bind-key 3 split-window -h

bind-key C-b select-pane -L
bind-key C-n select-pane -D
bind-key C-p select-pane -U
bind-key C-f select-pane -R

bind-key k kill-pane

# resize C-t + C-Up,Down...連打がサイズ変更

Emacs

外部エディタ起動で呼ばれた時に、毎回保存を確認されないように

(server-start)
(defun my/server-save-and-edit ()
  (interactive)
  (save-buffer)
  (server-edit))
(define-key ctl-x-map "#" #'my/server-save-and-edit)

各CLIにおける外部エディタ起動のショートカット

  • Claude Code: Ctrl-g
  • codex: 今ないけど Ctrl-g になるぽい
  • gemini-cli: Ctrl-x :rage:
  • qwen: Ctrl-x :rage:

CLIの切り替え

別バッファで編集しているファイル上からも Ctrl-tabで、別ウィンドウのAI(tmux)を切り替えてます。

;; ** 自分の環境でしか動かないかもなので参考までに **
(defun my-tmux-next-window ()
  "Switch to the next tmux window in the current EXWM workspace's corresponding tmux session."
  (interactive)
  (let ((current-workspace-index exwm-workspace-current-index))
    (call-process "tmux" nil 0 nil "next-window" "-t"
                 (number-to-string current-workspace-index))))

(defun my-tmux-previous-window ()
  "Switch to the previous tmux window in target session 0."
  (interactive)
  (let ((current-workspace-index exwm-workspace-current-index))
    (call-process "tmux" nil 0 nil "previous-window" "-t"
                 (number-to-string current-workspace-index))))

(global-set-key (kbd "<C-tab>") 'my-tmux-next-window)
(global-set-key (kbd "C-<iso-lefttab>") 'my-tmux-previous-window)

CLIへの送信

編集しているファイル上の 現在行 もしくは 選択範囲 のテキストをAI(tmux)に直接送信します。
フォーカスを変えず、ENTER含めていきなり実行 するのが便利かと思いつつも、全然使ってません。

;; ** 自分の環境でしか動かないかもなので参考までに **
(defun my-send-to-tmux ()
  "Send the selected region or current line to tmux pane 0, followed by Enter."
  (interactive)
  (let* ((contents
          (if (region-active-p)
              (buffer-substring-no-properties (region-beginning) (region-end))
            (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
         (escaped-contents (shell-quote-argument contents))
         (tmux-send-text-command (concat "tmux send-keys -t 0 -- " escaped-contents))
         (tmux-enter-command "tmux send-keys -t 0 Enter"))
    (shell-command tmux-send-text-command)
    (shell-command tmux-enter-command)))
(global-set-key (kbd "C-x <C-return>") 'my-send-to-tmux)

まとめ

gptelollamaもいれてはいますが、常用するに至っていません。
そんな使い方に、興味を持った方はぜひ試してみてください :bow_tone1:

:christmas_tree: Emacs Advent Calendar 2025
:arrow_left: 21日目 ai-code-interface.el について書きたいなと思っていましたが、落としてしまったので Emacs との出会いについてのポエムになりました (@kyre)
:arrow_right: 23日目 Emacsを使う理由(7): まとめ (@howking)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?