A Visual Studio Code extension that preserves (and adjusts) indentation when pasting multi-line code snippets. No more endless manual re-formatting!
Features
Indentation Preservation: Automatically detects the minimal common indentation in the pasted code and aligns it with your current cursor’s indentation level.
Multi-Cursor Support: Works seamlessly with multiple cursors, so each pasted snippet is adjusted appropriately.
Configurable Keybinding: By default, this extension listens on Ctrl+Shift+V (Windows/Linux) or Cmd+Shift+V (macOS), but you can easily override your default paste shortcut if you prefer.
How It Works
Copy any code snippet with indentation from another file or source.
Place your cursor where you want the code to be inserted in VS Code.
Trigger the Command:
By default, press Ctrl+Shift+V on Windows/Linux, or Cmd+Shift+V on macOS.
Result: Your pasted code is automatically re-indented to match your current line’s indentation.
Installation
From the Marketplace
Open VS Code, go to the Extensions panel.
Search for Paste With Indentation (or your extension name).
Click Install.
Reload Visual Studio Code if prompted.
From a VSIX File (Offline Installation)
If you have the .vsix package:
Open VS Code, go to the Extensions panel.
Click the … menu (top right) and select Install from VSIX.
Choose the downloaded .vsix file.
Reload Visual Studio Code if prompted.
Usage
After installation, when you want to preserve indentation while pasting, press Ctrl+Shift+V (Windows/Linux) or Cmd+Shift+V (macOS).
The extension detects the current line’s indentation, detects the minimal indentation in the copied snippet, and merges them so your code block remains aligned.
Updating Keybindings
If you want to override the default paste (Ctrl+V / Cmd+V) so that all paste actions use this extension’s functionality:
Open VS Code’s Command Palette
Windows/Linux: Ctrl+Shift+P
macOS: Cmd+Shift+P
Search for “Preferences: Open Keyboard Shortcuts (JSON)” and select it.
In the keybindings.json file that opens, add or edit entries like this:
[
// 1) Unbind the default paste action
{
"key": "ctrl+v",
"command": "-editor.action.clipboardPasteAction",
"when": "editorTextFocus"
},
// 2) Rebind Ctrl+V to the new pasteWithIndentation command
{
"key": "ctrl+v",
"command": "extension.pasteWithIndentation",
"when": "editorTextFocus"
}
]
Note:
Replace ctrl+v with cmd+v on macOS.
Adjust keys/shortcuts to your personal preference or OS.
Save the file, and your custom keybinding will take effect immediately.
Contributing
Feel free to open issues or pull requests on the GitHub repository if you encounter any bugs or want to request new features.