A Visual Studio Code extension that allows you to copy selected code or the entire content of a file as a Markdown, HTML, or plain text code snippet with language and file path information.
- Copy selected code or the entire file content
- Automatically includes the language identifier based on file type
- Optionally includes the relative file path from the workspace
- Supports Markdown, HTML, and plain text snippet formats
- Handles special cases (e.g.,
build.gradle→groovy,Dockerfile,docker-compose.yaml) - Clipboard-ready output for pasting into docs, chats, or markdown editors
- AI-friendly Markdown mode (opt-in) that adds file/language/range headers
- Fence strategy controls to avoid broken Markdown when code includes triple backticks or to force
~~~ - Optional head/tail sampling prompt for very large files, with presets and custom head/tail counts
- Open any file in VS Code
- Run the command "Copy Code as Snippet" using one of these methods:
- Press the keyboard shortcut (if configured)
- Open the Command Palette (
Ctrl+Shift+PorCmd+Shift+P) and search for "Copy Code as Snippet" - Right-click in the editor and select "Copy Code as Snippet" (if added to context menu)
- The snippet will be copied to your clipboard in the selected format:
Example (Markdown with file path)
```typescript:src/extension.ts
export function activate(context: vscode.ExtensionContext) {
// ...
}
```Example (AI mode header + range)
### File: src/extension.ts
### Language: typescript
### Range: lines 12-34 (selection)
```typescript
// selected code...
```Example (large file head/tail sample)
```javascript:src/huge-file.js
// first N lines...
... 9,940 lines omitted ...
// last M lines...
```This extension is particularly useful for:
- Creating rich code examples for documentation and presentations
- Sharing code in chats and forums with language-aware formatting
- Providing context to AI assistants with file-level information
- Pasting syntax-highlighted snippets in HTML/Markdown documents
No dependencies or special requirements.
| Setting | Type | Default | Description |
|---|---|---|---|
copy-code-as-snippet.includeFilePath |
boolean |
true |
Whether to include the relative file path in the snippet |
copy-code-as-snippet.format |
string (markdown, html, plain) |
markdown |
Output format for the snippet |
copy-code-as-snippet.aiMode.enabled |
boolean |
false |
Adds Markdown headers (file, language, range) and uses language-only fences (markdown only) |
copy-code-as-snippet.markdown.fenceStrategy |
string (default, autoUpgrade, tilde) |
default |
Fence style: keep triple backticks, auto-upgrade to four backticks when content has backticks, or always use ~~~ |
copy-code-as-snippet.largeFile.lineThreshold |
number |
1000 |
Line count threshold to treat a file as large |
copy-code-as-snippet.largeFile.promptEnabled |
boolean |
false |
When true and over threshold, prompt to copy full file or a head/tail sample (pick presets or enter custom counts) |
None at this time. Please report any issues via GitHub Issues.
- Added AI-friendly Markdown mode with file/language/range header (opt-in)
- Added Markdown fence strategies: default, auto-upgrade when ``` appears, or tilde fences
- Added large-file prompt to choose full copy or head/tail sampling, including presets or custom head/tail counts
- Default settings keep 1.1.x behavior unchanged
- Added support for:
- Snippet format options:
markdown,html, andplain text - Selecting code instead of copying the full file
- Customizable settings for file path inclusion and output format
- Snippet format options:
- Improved workspace-relative path detection in multi-root environments
- Improved HTML escaping for enhanced security
- Initial release
- Support for copying entire file content as a Markdown code snippet with language and file path information
npm install
npm run compile- Press
F5to open a new VS Code window with your extension loaded - Run the command "Copy Code as Snippet"
- Verify that the clipboard contains the expected snippet format
vsce package
vsce publish