This is a powerful VSCode extension that allows you to copy selected code to the clipboard in an AI-friendly format, optionally including context information to make it easier for AI tools to understand your code.
This extension provides three main features, allowing you to choose the one that suits your needs:
- Preserve Relative Indentation - Removes common leading spaces to align the code neatly
- Convert to Markdown Format - Automatically adds syntax highlighting
- Include File Path and Line Numbers - Provides basic context for the code
In addition to the basic features, it also provides:
- Code Structure Analysis - Automatically identifies the function, class, namespace, etc., where the selected area is located
- Related Imports Identification - Intelligently finds import statements related to the selected code
- Multiple Output Formats - Supports Markdown, XML, JSON, and custom formats
- Multi-language Support - Supports most mainstream programming languages
A new feature that allows you to easily select and copy multiple files:
- Multi-file/Snippet Selection - Use the file explorer interface to select multiple files or code snippets.
- Batch Folder Selection - Selecting a folder automatically selects all its child files.
- File/Snippet Search Filter - Quickly filter and display the files or snippets you need.
- Tokens Estimation - Displays the estimated total number of tokens and percentage for the selected items (files + snippets).
- State Persistence - Remembers your selection, expansion state, and saved snippets.
- Context Menu Integration - Supports adding files/folders directly from the file explorer and editor tabs.
- Range Selection - Supports holding down the Shift key and clicking the checkbox to quickly select multiple files or snippets within a range.
- Item Selection - Supports clicking the entire row in the file or snippet list to toggle the selection state, making the operation more intuitive.
- Code Snippet Support (Snippets):
- Select code from the editor, right-click and choose "Add Snippet to Copy For AI Explorer" to save the snippet.
- Manage snippets in a separate "Code Snippets" section in the explorer.
- Supports selecting, copying, previewing (click to jump to the source code), and removing snippets.
- Snippets will save context information (optional, based on settings).
- Collapsible Sections: The file list and code snippet list are now in independently collapsible sections.
You can use this feature in the following ways:
Context Menu:
- Select code in the editor
- Right-click the selected text
- Choose the "Copy For AI" option
Keyboard Shortcut:
- Select code in the editor
- Press
Ctrl+Alt+C(Windows/Linux) orCmd+Alt+C(Mac)
Command Palette:
- Select code in the editor
- Press
Ctrl+Shift+PorCmd+Shift+Pto open the command palette - Type "Copy For AI" and choose the command
Context Menu:
- Select code in the editor
- Right-click the selected text
- Choose the "Copy For AI (With Context)" option
Keyboard Shortcut:
- Select code in the editor
- Press
Ctrl+Alt+Shift+C(Windows/Linux) orCmd+Alt+Shift+C(Mac)
Command Palette:
- Select code in the editor
- Press
Ctrl+Shift+PorCmd+Shift+Pto open the command palette - Type "Copy For AI (With Context)" and choose the command
Open the Explorer:
- Click the "Copy For AI" icon in the activity bar on the left to open the explorer sidebar
Select Files and Snippets:
- Check the files, folders, or code snippets you want to copy.
- Checking a folder will automatically select all its child files.
- Range Selection: Click the checkbox of the first item (file or snippet), then hold down the Shift key and click the checkbox of another item to select all visible items between them.
Add Items Using the Context Menu:
- In the VSCode file explorer, right-click a file and choose "Add File to Copy For AI Explorer"
- In the VSCode file explorer, right-click a folder and choose "Add Folder to Copy For AI Explorer"
- Right-click a tab in the editor and choose "Add Tab to Copy For AI Explorer"
- New: Select code in the editor, right-click the selected text, and choose "Add Snippet to Copy For AI Explorer"
Filter Items:
- Enter keywords in the search box at the top to quickly filter files or snippets (supports case-insensitive and multi-keyword search).
- Check "Show Selected Only" to focus on the selected items
Copy Items:
- Click the "Copy to Clipboard" button at the bottom
- All selected files and snippet contents will be copied to the clipboard in the specified format
Manage Snippets:
- In the "Code Snippets" section, you can check/uncheck snippets.
- Clicking a snippet row (not the checkbox) will jump to the source code location and select the corresponding range.
- Clicking the trash icon on the right side of the snippet row will remove the snippet.
Then paste the copied content into ChatGPT, Claude, or other AI tools to maintain the code format and context.
## File: extension.ts (10-20)
```typescript
function activate(context) {
// Code content
}
```# CODE CONTEXT
-----------------
## File
extension.ts (10-20)
## Structure
- Function: activate(context: vscode.ExtensionContext)
## Imports
```typescript
import * as vscode from 'vscode';
```
## Code
```typescript
function activate(context) {
// Code content
}
```
-----------------## File: src/extension.ts
```typescript
import * as vscode from 'vscode';
import { processCode, removeComments } from './codeAnalyzer';
import { formatOutput } from './formatter';
export function activate(context: vscode.ExtensionContext) {
console.log('Extension "copy-for-ai" activated!');
}
```
## Snippet: src/formatter.ts (5-15)
### Structure
- Function: formatOutput(options: FormatOptions)
### Imports
```typescript
// ... relevant imports for the snippet ...
```
### Code
```typescript
export function formatOutput(options: FormatOptions): string {
switch (options.format) {
// ... snippet code ...
}
}
```
## File: src/formatter.ts
```typescript
export function formatOutput(options: FormatOptions): string {
switch (options.format) {
case OutputFormat.Markdown:
return formatAsMarkdown(options);
case OutputFormat.XML:
return formatAsXML(options);
default:
return formatAsMarkdown(options);
}
}
```You can customize the behavior of the extension in the VSCode settings:
-
copyForAI.includeStructureInfo (default: true)
- Whether to include code structure information
-
copyForAI.includeRelatedImports (default: true)
- Whether to include related import statements
-
copyForAI.outputFormat (default: "markdown")
- The output format for context information
- Options: "markdown", "xml", "json", "custom"
-
copyForAI.customFormatBefore (default: "===== CODE CONTEXT START =====")
- Custom format start marker
-
copyForAI.customFormatAfter (default: "===== CODE CONTEXT END =====")
- Custom format end marker
-
copyForAI.includeComments (default: true)
- Whether to include comments in the code
-
copyForAI.tokenLimit (default: 0)
- Default token limit (0 means no limit)
-
copyForAI.contextExplorer.excludePatterns (default: ["/node_modules/", "/.git/", "/dist/", "/build/", "/bin/"])
- File patterns to exclude in the Context Explorer
-
copyForAI.contextExplorer.followGitignore (default: true)
- Whether the Context Explorer should follow the .gitignore file rules in the workspace root.
Install from the VSCode extension marketplace:
- Open VSCode
- Press
Ctrl+Shift+XorCmd+Shift+Xto open the extensions view - Search for "Copy For AI"
- Click "Install"
If you want to install manually:
- Download the
.vsixfile - In VSCode, press
Ctrl+Shift+XorCmd+Shift+Xto open the extensions view - Click the "..." button in the top right corner
- Choose "Install from VSIX..."
- Select the downloaded
.vsixfile
The extension supports all languages supported by VSCode and provides enhanced context analysis for the following languages:
- JavaScript / TypeScript
- Python
- Java
- C#
- C / C++
- Go
- Ruby
- PHP
- Swift
- Rust
- Kotlin
# Clone the repository
git clone <your-repo-url>
cd copy-for-ai
# Install dependencies
npm installnpm run compilenpm run watch# Method 1: Using F5 key
# Open the project in VSCode, then press F5 to start a new VSCode window for testing
# Method 2: Using command line
code --extensionDevelopmentPath=${PWD}# Make sure you have vsce installed
npm install -g @vscode/vsce
# Package the extension into a .vsix file
vsce packageThis will generate a copy-for-ai-0.1.8.vsix file in the project root directory (version number may vary).
MIT