1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
# claude-code-context.el
Share your Emacs buffer context automatically with [Claude Code](https://github.com/anthropics/claude-code).
## Features
- **Automatic context sharing**: Automatically sends your current file, line number, column, and text selection to Claude Code
- **Flymake integration**: Optionally include linter/diagnostic output in the context
- **Idle-timer based**: Updates context only when Emacs is idle to avoid performance impact
- **Simple keybindings**: Easy-to-use commands for manual control
## Installation
### From MELPA (coming soon)
```elisp
(use-package claude-code-context
:config
(claude-code-context-mode 1))
```
### Manual Installation
1. Clone this repository:
```bash
git clone https://github.com/lhoersten/claude-code-context.git
```
2. Add to your Emacs configuration:
```elisp
(add-to-list 'load-path "/path/to/claude-code-context")
(require 'claude-code-context)
(claude-code-context-mode 1)
```
## Configuration
### Emacs Setup
Add to your `init.el`:
```elisp
(require 'claude-code-context)
;; Enable automatic context updates
(claude-code-context-mode 1)
;; Optional: customize the update interval (default: 2 seconds)
(setq claude-code-context-update-interval 3)
;; Optional: customize the context file location
(setq claude-code-context-file "~/.emacs.d/claude-code-context.json")
```
### Claude Code Hook Setup
#### Option A: Automatic via `/update-config` skill
If you use [Claude Code](https://claude.ai/code), you can set up the hook automatically. Type this in Claude Code:
```
/update-config Add a UserPromptSubmit hook that reads ~/.config/emacs/claude-code-context.json and injects it into the prompt
```
Claude Code's `update-config` skill will read your existing `~/.claude/settings.json`, merge in the hook safely, and validate the result.
#### Option B: Manual
Add this hook configuration to your `~/.claude/settings.json`:
```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"
}
]
}
]
}
}
```
See [claude-code-hook-example.json](claude-code-hook-example.json) for a complete example.
## Usage
### Automatic Mode
When `claude-code-context-mode` is enabled, your current buffer context is automatically written to `~/.emacs.d/claude-code-context.json` every time Emacs is idle for the configured interval.
### Manual Commands
| Keybinding | Command | Description |
|-------------|---------------------------------|------------------------------------------------|
| `C-c C-l u` | `claude-code-context-update-context` | Manually update context |
| `C-c C-l d` | `claude-code-context-add-diagnostics` | Add flymake diagnostics to context |
| `C-c C-l c` | `claude-code-context-clear-context` | Clear the context file |
| `C-c C-l m` | `claude-code-context-mode` | Toggle automatic context mode |
### Workflow Example
1. Open a file in Emacs
2. Position your cursor where you want help
3. Optionally select some code
4. Use `C-c C-l d` to include any linter errors
5. Switch to your terminal and ask Claude Code a question
6. Claude Code will automatically see your Emacs context!
## Context Format
The context file is JSON format for easy parsing and includes:
- **buffer**: Full path to the current buffer's file, or buffer name for non-file buffers (e.g. `*Messages*`). Selections work in all buffers — select the relevant text to include it in context.
- **line**: Current line number
- **column**: Current column number
- **modified**: Boolean indicating if the buffer has unsaved changes
- **selection**: Any highlighted text (if region is active)
- **diagnostics**: Flymake errors/warnings (when using `C-c C-l d`)
Example context:
```json
{
"buffer": "/Users/username/project/src/main.py",
"line": 42,
"column": 8,
"modified": true,
"selection": "def process_data(items):\n return [x * 2 for x in items]",
"diagnostics": [
{
"line": 45,
"type": "error",
"backend": "eglot",
"text": "undefined variable 'result'"
},
{
"line": 52,
"type": "warning",
"backend": "eglot",
"text": "unused variable 'temp'"
}
]
}
```
## Customization
### Variables
- `claude-code-context-file`: Location of the context file (default: `~/.emacs.d/claude-code-context.json`)
- `claude-code-context-update-interval`: Idle time in seconds before updating context (default: 2)
### Custom Keybindings
If you prefer different keybindings:
```elisp
(global-set-key (kbd "C-c u") 'claude-code-update-context)
(global-set-key (kbd "C-c d") 'claude-code-add-diagnostics)
```
## How It Works
1. **Emacs side**: This package writes your current buffer context to a JSON file (`~/.emacs.d/claude-code-context.json`)
2. **Claude Code side**: A hook in Claude Code's settings reads this JSON file before every prompt submission
3. **Integration**: Claude Code automatically receives and parses your Emacs context with each query
## Troubleshooting
**Context not appearing in Claude Code:**
- Verify the hook is properly configured in `~/.claude/settings.json`
- Check that the context file exists: `cat ~/.emacs.d/claude-code-context.json`
- Make sure `claude-code-context-mode` is enabled in Emacs
**Performance issues:**
- Increase `claude-code-context-update-interval` to reduce update frequency
- Disable automatic mode and use manual commands only
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## License
GPL-3.0-or-later
## Author
Luke Hoersten <[email protected]>
|