File size: 10,412 Bytes
064bfd6 | 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | import { DEFAULT_BINDINGS } from '../../keybindings/defaultBindings.js'
import { isKeybindingCustomizationEnabled } from '../../keybindings/loadUserBindings.js'
import {
MACOS_RESERVED,
NON_REBINDABLE,
TERMINAL_RESERVED,
} from '../../keybindings/reservedShortcuts.js'
import type { KeybindingsSchemaType } from '../../keybindings/schema.js'
import {
KEYBINDING_ACTIONS,
KEYBINDING_CONTEXT_DESCRIPTIONS,
KEYBINDING_CONTEXTS,
} from '../../keybindings/schema.js'
import { jsonStringify } from '../../utils/slowOperations.js'
import { registerBundledSkill } from '../bundledSkills.js'
/**
* Build a markdown table of all contexts.
*/
function generateContextsTable(): string {
return markdownTable(
['Context', 'Description'],
KEYBINDING_CONTEXTS.map(ctx => [
`\`${ctx}\``,
KEYBINDING_CONTEXT_DESCRIPTIONS[ctx],
]),
)
}
/**
* Build a markdown table of all actions with their default bindings and context.
*/
function generateActionsTable(): string {
// Build a lookup: action -> { keys, context }
const actionInfo: Record<string, { keys: string[]; context: string }> = {}
for (const block of DEFAULT_BINDINGS) {
for (const [key, action] of Object.entries(block.bindings)) {
if (action) {
if (!actionInfo[action]) {
actionInfo[action] = { keys: [], context: block.context }
}
actionInfo[action].keys.push(key)
}
}
}
return markdownTable(
['Action', 'Default Key(s)', 'Context'],
KEYBINDING_ACTIONS.map(action => {
const info = actionInfo[action]
const keys = info ? info.keys.map(k => `\`${k}\``).join(', ') : '(none)'
const context = info ? info.context : inferContextFromAction(action)
return [`\`${action}\``, keys, context]
}),
)
}
/**
* Infer context from action prefix when not in DEFAULT_BINDINGS.
*/
function inferContextFromAction(action: string): string {
const prefix = action.split(':')[0]
const prefixToContext: Record<string, string> = {
app: 'Global',
history: 'Global or Chat',
chat: 'Chat',
autocomplete: 'Autocomplete',
confirm: 'Confirmation',
tabs: 'Tabs',
transcript: 'Transcript',
historySearch: 'HistorySearch',
task: 'Task',
theme: 'ThemePicker',
help: 'Help',
attachments: 'Attachments',
footer: 'Footer',
messageSelector: 'MessageSelector',
diff: 'DiffDialog',
modelPicker: 'ModelPicker',
select: 'Select',
permission: 'Confirmation',
}
return prefixToContext[prefix ?? ''] ?? 'Unknown'
}
/**
* Build a list of reserved shortcuts.
*/
function generateReservedShortcuts(): string {
const lines: string[] = []
lines.push('### Non-rebindable (errors)')
for (const s of NON_REBINDABLE) {
lines.push(`- \`${s.key}\` β ${s.reason}`)
}
lines.push('')
lines.push('### Terminal reserved (errors/warnings)')
for (const s of TERMINAL_RESERVED) {
lines.push(
`- \`${s.key}\` β ${s.reason} (${s.severity === 'error' ? 'will not work' : 'may conflict'})`,
)
}
lines.push('')
lines.push('### macOS reserved (errors)')
for (const s of MACOS_RESERVED) {
lines.push(`- \`${s.key}\` β ${s.reason}`)
}
return lines.join('\n')
}
const FILE_FORMAT_EXAMPLE: KeybindingsSchemaType = {
$schema: 'https://www.schemastore.org/claude-code-keybindings.json',
$docs: 'https://code.claude.com/docs/en/keybindings',
bindings: [
{
context: 'Chat',
bindings: {
'ctrl+e': 'chat:externalEditor',
},
},
],
}
const UNBIND_EXAMPLE: KeybindingsSchemaType['bindings'][number] = {
context: 'Chat',
bindings: {
'ctrl+s': null,
},
}
const REBIND_EXAMPLE: KeybindingsSchemaType['bindings'][number] = {
context: 'Chat',
bindings: {
'ctrl+g': null,
'ctrl+e': 'chat:externalEditor',
},
}
const CHORD_EXAMPLE: KeybindingsSchemaType['bindings'][number] = {
context: 'Global',
bindings: {
'ctrl+k ctrl+t': 'app:toggleTodos',
},
}
const SECTION_INTRO = [
'# Keybindings Skill',
'',
'Create or modify `~/.claude/keybindings.json` to customize keyboard shortcuts.',
'',
'## CRITICAL: Read Before Write',
'',
'**Always read `~/.claude/keybindings.json` first** (it may not exist yet). Merge changes with existing bindings β never replace the entire file.',
'',
'- Use **Edit** tool for modifications to existing files',
'- Use **Write** tool only if the file does not exist yet',
].join('\n')
const SECTION_FILE_FORMAT = [
'## File Format',
'',
'```json',
jsonStringify(FILE_FORMAT_EXAMPLE, null, 2),
'```',
'',
'Always include the `$schema` and `$docs` fields.',
].join('\n')
const SECTION_KEYSTROKE_SYNTAX = [
'## Keystroke Syntax',
'',
'**Modifiers** (combine with `+`):',
'- `ctrl` (alias: `control`)',
'- `alt` (aliases: `opt`, `option`) β note: `alt` and `meta` are identical in terminals',
'- `shift`',
'- `meta` (aliases: `cmd`, `command`)',
'',
'**Special keys**: `escape`/`esc`, `enter`/`return`, `tab`, `space`, `backspace`, `delete`, `up`, `down`, `left`, `right`',
'',
'**Chords**: Space-separated keystrokes, e.g. `ctrl+k ctrl+s` (1-second timeout between keystrokes)',
'',
'**Examples**: `ctrl+shift+p`, `alt+enter`, `ctrl+k ctrl+n`',
].join('\n')
const SECTION_UNBINDING = [
'## Unbinding Default Shortcuts',
'',
'Set a key to `null` to remove its default binding:',
'',
'```json',
jsonStringify(UNBIND_EXAMPLE, null, 2),
'```',
].join('\n')
const SECTION_INTERACTION = [
'## How User Bindings Interact with Defaults',
'',
'- User bindings are **additive** β they are appended after the default bindings',
'- To **move** a binding to a different key: unbind the old key (`null`) AND add the new binding',
"- A context only needs to appear in the user's file if they want to change something in that context",
].join('\n')
const SECTION_COMMON_PATTERNS = [
'## Common Patterns',
'',
'### Rebind a key',
'To change the external editor shortcut from `ctrl+g` to `ctrl+e`:',
'```json',
jsonStringify(REBIND_EXAMPLE, null, 2),
'```',
'',
'### Add a chord binding',
'```json',
jsonStringify(CHORD_EXAMPLE, null, 2),
'```',
].join('\n')
const SECTION_BEHAVIORAL_RULES = [
'## Behavioral Rules',
'',
'1. Only include contexts the user wants to change (minimal overrides)',
'2. Validate that actions and contexts are from the known lists below',
'3. Warn the user proactively if they choose a key that conflicts with reserved shortcuts or common tools like tmux (`ctrl+b`) and screen (`ctrl+a`)',
'4. When adding a new binding for an existing action, the new binding is additive (existing default still works unless explicitly unbound)',
'5. To fully replace a default binding, unbind the old key AND add the new one',
].join('\n')
const SECTION_DOCTOR = [
'## Validation with /doctor',
'',
'The `/doctor` command includes a "Keybinding Configuration Issues" section that validates `~/.claude/keybindings.json`.',
'',
'### Common Issues and Fixes',
'',
markdownTable(
['Issue', 'Cause', 'Fix'],
[
[
'`keybindings.json must have a "bindings" array`',
'Missing wrapper object',
'Wrap bindings in `{ "bindings": [...] }`',
],
[
'`"bindings" must be an array`',
'`bindings` is not an array',
'Set `"bindings"` to an array: `[{ context: ..., bindings: ... }]`',
],
[
'`Unknown context "X"`',
'Typo or invalid context name',
'Use exact context names from the Available Contexts table',
],
[
'`Duplicate key "X" in Y bindings`',
'Same key defined twice in one context',
'Remove the duplicate; JSON uses only the last value',
],
[
'`"X" may not work: ...`',
'Key conflicts with terminal/OS reserved shortcut',
'Choose a different key (see Reserved Shortcuts section)',
],
[
'`Could not parse keystroke "X"`',
'Invalid key syntax',
'Check syntax: use `+` between modifiers, valid key names',
],
[
'`Invalid action for "X"`',
'Action value is not a string or null',
'Actions must be strings like `"app:help"` or `null` to unbind',
],
],
),
'',
'### Example /doctor Output',
'',
'```',
'Keybinding Configuration Issues',
'Location: ~/.claude/keybindings.json',
' β [Error] Unknown context "chat"',
' β Valid contexts: Global, Chat, Autocomplete, ...',
' β [Warning] "ctrl+c" may not work: Terminal interrupt (SIGINT)',
'```',
'',
'**Errors** prevent bindings from working and must be fixed. **Warnings** indicate potential conflicts but the binding may still work.',
].join('\n')
export function registerKeybindingsSkill(): void {
registerBundledSkill({
name: 'keybindings-help',
description:
'Use when the user wants to customize keyboard shortcuts, rebind keys, add chord bindings, or modify ~/.claude/keybindings.json. Examples: "rebind ctrl+s", "add a chord shortcut", "change the submit key", "customize keybindings".',
allowedTools: ['Read'],
userInvocable: false,
isEnabled: isKeybindingCustomizationEnabled,
async getPromptForCommand(args) {
// Generate reference tables dynamically from source-of-truth arrays
const contextsTable = generateContextsTable()
const actionsTable = generateActionsTable()
const reservedShortcuts = generateReservedShortcuts()
const sections = [
SECTION_INTRO,
SECTION_FILE_FORMAT,
SECTION_KEYSTROKE_SYNTAX,
SECTION_UNBINDING,
SECTION_INTERACTION,
SECTION_COMMON_PATTERNS,
SECTION_BEHAVIORAL_RULES,
SECTION_DOCTOR,
`## Reserved Shortcuts\n\n${reservedShortcuts}`,
`## Available Contexts\n\n${contextsTable}`,
`## Available Actions\n\n${actionsTable}`,
]
if (args) {
sections.push(`## User Request\n\n${args}`)
}
return [{ type: 'text', text: sections.join('\n\n') }]
},
})
}
/**
* Build a markdown table from headers and rows.
*/
function markdownTable(headers: string[], rows: string[][]): string {
const separator = headers.map(() => '---')
return [
`| ${headers.join(' | ')} |`,
`| ${separator.join(' | ')} |`,
...rows.map(row => `| ${row.join(' | ')} |`),
].join('\n')
}
|