I like to use Claude Code in my terminal Ghostty alongside Emacs. Claude can already read and edit files directly, and Emacs auto-reverts when it does. But Claude doesn't know my buffer state like which file I have open, which line im at, and what I have highlighted.

claude-code-context (github) does just that.

How It Works

The package runs an idle timer that writes your current buffer state to a JSON file every 2 seconds:

{
  "buffer": "/Users/luke/project/src/main.py",
  "line": 42,
  "column": 8,
  "modified": false,
  "selection": "def process_data(items):\n    return [x * 2 for x in items]"
}

A UserPromptSubmit hook in Claude Code's ~/.claude/settings.json injects that file into every prompt automatically. Claude sees your cursor position and active selection without you having to copy-paste anything.

It works in both file buffers and transient buffers like *Messages* and *compilation*.

The timer only fires when Emacs is idle so there's no performance impact during active editing. Use C-c C-l d to optionally append flymake diagnostics so Claude can see specific errors. All commands are also available manually for full control without the timer.

Setup

(add-to-list 'load-path "/path/to/claude-code-context")
(require 'claude-code-context)
(claude-code-context-mode 1)

Add to ~/.claude/settings.json:

{
  "hooks": {
    "UserPromptSubmit": [{
      "hooks": [{
        "type": "command",
        "command": "CONTEXT_FILE=\"${XDG_CONFIG_HOME:-$HOME/.config}/emacs/claude-code-context.json\"; [ -f \"$CONTEXT_FILE\" ] || CONTEXT_FILE=\"$HOME/.emacs.d/claude-code-context.json\"; if [ -f \"$CONTEXT_FILE\" ]; then echo \"\\n---\\n## Emacs Context\\n\"; cat \"$CONTEXT_FILE\"; echo \"\\n---\"; fi"
      }]
    }]
  }
}

The hook detects both ~/.config/emacs (XDG) and traditional ~/.emacs.d automatically.

Keybindings

KeyAction
C-c C-l uManually update context
C-c C-l dInclude flymake diagnostics
C-c C-l cClear context
C-c C-l mToggle auto-update mode

The package is submitted to MELPA.