A VS Code extension that turns any highlighted text into an AI-powered exchange with Claude — instantly, from a right-click. Select text anywhere in your editor, choose Claude: Save Selected Text from the context menu, and Claude reasons through it and responds in a new tab.
Save Selected Text is a TypeScript VS Code extension that wires the Anthropic API directly into your editing workflow. It follows the ETL (Extract, Transform, Load) pattern — the same architecture used in enterprise integration platforms like Workato, Boomi, and MuleSoft:
Your extension is the plumbing. Claude is the brain.
- Extract — capture the selected text from the active editor via the VS Code Selection API
- Transform — send it through Claude's reasoning engine via the Anthropic SDK, which generates a response that didn't exist before
- Load — save the selected text to a timestamped
.txtfile and display Claude's response in a new Markdown tab
Claude doesn't look up answers or retrieve pre-written responses. It reasons through what you selected, applies deduction and contextual understanding, and generates something new. The result opens right next to your code.
This extension implements a write-on-success pattern — a core principle in integration engineering. The selected text is only saved to disk after Claude responds successfully. If the API call fails, nothing gets written. No orphaned files, no half-finished state.
This mirrors how database transactions work in production systems — nothing gets committed until the full operation succeeds.
- Right-click context menu — highlight any text, right-click, and run Claude: Save Selected Text instantly
- Write-on-success — files are only saved after a confirmed Claude response
- Timestamped output files — each selection is saved as
prompt-[timestamp].txtin your workspace'sprompts/folder - Model dropdown — choose between Claude Haiku, Sonnet, and Opus in VS Code settings
- Progress indicator — a notification bar shows while Claude is reasoning through your selection
- Structured output — responses open in a new Markdown tab showing your selected text and Claude's reply side by side
- Secure API key storage — your Anthropic API key is stored in VS Code settings, never hardcoded
Claude is not a search engine. It doesn't retrieve pre-written answers. When your extension sends selected text to the API, Claude draws on everything it was trained on to reason through the input and generate a novel response.
Every API call is stateless — Claude starts fresh each time with no memory of previous selections. This makes the pipeline predictable and repeatable, which is exactly what you want in an automated workflow.
Practically, this means Claude can:
- Summarize complex text into plain English
- Classify content into categories without a predefined list
- Deduce intent from loosely written notes and respond appropriately
- Generate structured output — analyses, plans, explanations — from unstructured input
- Answer questions by applying contextual reasoning, not keyword matching
- TypeScript — strict mode, Node16 module resolution
- VS Code Extension API —
registerCommand,activeTextEditor,withProgress,openTextDocument - Anthropic SDK (
@anthropic-ai/sdk) —claude-sonnet-4-6model (configurable) - Node.js
fsmodule — file system writes with write-on-success pattern
git clone https://github.com/kaidez/save-selected-text.git
cd save-selected-textnpm install- Open VS Code Settings (
Cmd+,) - Search for Save Selected Text
- Paste your Anthropic API key into the Api Key field
Don't have a key? Get one at console.anthropic.com.
npm run compilePress Fn+F5 (Mac) to launch the Extension Development Host.
In the dev host window, open any file with text in it, highlight a sentence or paragraph, right-click, and select Claude: Save Selected Text.
- Open any file in VS Code
- Highlight any text with your mouse
- Right-click and select Claude: Save Selected Text
- A progress notification appears while Claude reasons through your selection
- A new Markdown tab opens with your selected text and Claude's response
- A timestamped
.txtfile is saved to your workspace'sprompts/folder
SELECTED TEXT:
The write-on-success pattern ensures that data is only persisted after
a confirmed successful response from the external system.
---
CLAUDE'S RESPONSE:
This is a core principle in integration engineering — analogous to
database transaction commits. By deferring the write operation until
after the external API confirms success, the system avoids orphaned
records and maintains data integrity even when downstream services
are unavailable or return errors.
save-selected-text/
├── .vscode/
│ ├── extensions.json
│ ├── launch.json
│ └── tasks.json
├── out/ # compiled JS output
├── prompts/ # saved selections appear here
├── src/
│ ├── extension.ts # all extension logic
│ └── test/
│ ├── runTest.ts # test runner entry point
│ └── suite/
│ ├── index.ts # test suite loader
│ └── extension.test.ts # 9 unit tests
├── .gitignore
├── package.json
├── tsconfig.json
└── README.md
This project has 9 unit tests covering:
- Extension presence and command registration
- Guard clause behavior when no text is selected
- Timestamped filename generation and format validation
- File content accuracy — saved text matches selected text exactly
- Write-on-success pattern — file only created after successful API response
- Write-on-failure pattern — file NOT created when API call fails
- Claude API response parsing — text extraction and fallback handling
- Model dropdown configuration and valid default
npm testThe architecture of this extension mirrors patterns used in production automation platforms every day:
- The right-click command trigger is equivalent to a user-initiated webhook or manual trigger in Workato
- The Anthropic SDK call is equivalent to calling an external AI enrichment service in a Boomi process
- The write-on-success file save is equivalent to a conditional commit in an enterprise data pipeline
- The timestamped output file is equivalent to writing an enriched record to a destination system
Building this from scratch in TypeScript demonstrates an understanding of the architecture, not just the tooling.
This extension is one of two AI-powered VS Code tools in this portfolio. The companion project — claude-prompt-reader — uses an event-driven file watcher to automatically process prompts on save.
Together they demonstrate two fundamental automation trigger patterns:
| Project | Trigger Type | Pattern |
|---|---|---|
claude-prompt-reader |
File save event | Event-driven, automatic |
save-selected-text |
User right-click | Command-triggered, intentional |
Both follow the same ETL architecture. Both connect to the Anthropic API. Both have full unit test suites.
- Conversation history — maintain context across multiple selections so Claude can reason over a growing thread
- Multiple model support — let users configure different models for different selection types
- Webhook output — POST enriched responses to an external endpoint in addition to saving locally
- Batch processing — select multiple text blocks and process them as a single enriched pipeline
Kai Gittens kaidez.com