Claude Add Selection
Source: https://github.com/xiangyu123/claude-add-selection (private)
Marketplace: xuzhigui.claude-add-selection
A tiny local VSCode extension that adds an editor context menu item:
right-click selected code -> "Custom: Add Selection to Chat" (Chinese UI:
"自定义: 添加选中代码到聊天"). The Custom: / 自定义: prefix distinguishes it
from VSCode's built-in "Add Selection to Chat" (将所选内容添加到聊天) item.
The selection is inserted into the Claude Code chat input as an
@path#Lstart-Lend mention reference.
Why
The official Claude Code extension (anthropic.claude-code) only contributes
"Add to Chat" to the explorer and SCM context menus (whole files). Editor
selections are normally added via the Option+K shortcut
(claude-vscode.insertAtMention). This extension exposes the same behavior as
a right-click menu item, which is easier to discover.
How
The contributed command claude-add-selection.addToChat delegates to the
official command:
- native GUI mode:
claude-vscode.insertAtMention
- terminal mode (
claudeCode.useTerminal): claude-code.insertAtMentioned
No private API is used; the official command reads the active editor selection
and formats the mention itself, so behavior stays consistent across Claude
Code updates.
Note: the mention is a reference (file + line range), not pasted code text.
Claude Code reads the referenced lines when the message is sent. Editor
selections are also automatically shared as IDE context with each message.
Duplicate guard
Adding the identical selection (same file + line range) twice inserts the
reference only once; the second add is skipped with an info toast. Adding a
different selection re-arms the guard. The guard remembers the last added
selection per window: after sending the message, re-adding the very same
selection requires adding a different selection first (or reloading the
window) to reset it.
Prerequisites
- VSCode >= 1.85 (
code --version; the code CLI must be on PATH).
- Claude Code extension (
anthropic.claude-code) installed and enabled -
this extension delegates to its insertAtMention command.
- Node.js >= 20 only for building / testing / publishing from source
(
make install, make test, make publish). Installing the prebuilt
.vsix committed in this repo needs no Node at all.
make e2e additionally needs macOS with the Orca computer-use daemon
(accessibility + screen recording permissions); it is optional.
Install from a git clone (no Marketplace)
git clone https://github.com/xiangyu123/claude-add-selection.git
cd claude-add-selection
code --install-extension claude-add-selection-*.vsix # prebuilt, committed
code --list-extensions --show-versions | grep claude-add-selection
The repo ships an agent skill with the full procedure and troubleshooting:
skills/local-install/SKILL.md. CLI install
notifies running windows, so no reload is needed.
Install / Uninstall
From the VSCode Marketplace (published as xuzhigui.claude-add-selection):
search "Claude Add Selection" in the Extensions view, or
code --install-extension xuzhigui.claude-add-selection
Other machines signed into the same VSCode Settings Sync account pick it up
automatically. New publishes may stay in "Verifying" state for a few minutes
before the gallery page and CLI install become available.
Local development install (vsix built from this checkout):
make install # vsce package + code --install-extension (notifies running windows)
make uninstall
Usage
- Select code in any text editor.
- Right-click -> "Custom: Add Selection to Chat" (near the bottom of the menu).
- The
@file#Lstart-Lend reference appears in the Claude Code chat input.
Also available from the Command Palette as
"Claude Code: Custom: Add Selection to Chat" when a selection exists.
Development
make test # unit tests with a stubbed vscode module (node --test)
make e2e # end-to-end UI test in a real VSCode window (AppleScript-driven)
.vscode/launch.json provides an Extension Host debug configuration
(F5 -> "Run Extension").