Skip to content

Instantly share code, notes, and snippets.

@hyrious
Created October 28, 2025 10:34
Show Gist options
  • Select an option

  • Save hyrious/2e6900b4f97d0e9932ee08df706f462e to your computer and use it in GitHub Desktop.

Select an option

Save hyrious/2e6900b4f97d0e9932ee08df706f462e to your computer and use it in GitHub Desktop.
import * as vscode from 'vscode';
import redent from 'redent';
export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('selfhost.pasteWithIndent', async () => {
const text = await vscode.env.clipboard.readText()
if (!text) return;
const editor = vscode.window.activeTextEditor
if (editor) {
// Get cursor indentation by calculating the whitespace before the cursor
let cursorLine = editor.document.lineAt(editor.selection.active.line);
let cursorIndent = cursorLine.text.substring(0, editor.selection.active.character).match(/^\s*/)?.[0] || '';
// Redent the clipboard text to match the cursor indentation
const redentedText = redent(text, cursorIndent.length).trimStart();
editor.edit(b => b.insert(editor.selection.active, redentedText))
}
})
}
/** package.json
"contributes": {
"commands": [
{
"command": "selfhost.pasteWithIndent",
"title": "Selfhost: Paste With Indent"
}
],
"keybindings": [
{
"command": "selfhost.pasteWithIndent",
"key": "cmd+shift+v",
"when": "editorTextFocus"
}
]
},
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment