Settings to copy paste with correct indentation in Visual Studio Code

Visual Studio-CodeVscode Settings

Visual Studio-Code Problem Overview


When I copy and paste a piece of code using Visual Studio Code, after pressing enter to create a new line, the following happens:

enter image description here

It seems to keep the indentation created by the new line, adding the previous information from the copied snippet on top of it. I'd rather the code was auto-beautified on paste.

Can this behavior be changed through settings?

EDIT: I'm not referring to cross-platform copy-pasting, I'm referring to copying and pasting within the same file in Visual Studio Code.

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

This is already integrated in vscode with "editor.formatOnPaste": true property in settings.

Solution 2 - Visual Studio-Code

This plugin may help you:

paste-and-indent extension

You just have to change the shortcut and it works. Remember that you probably want to use 'cmd' instead of 'ctrl' if you're on a mac. Like this:

{
    "key": "ctrl+v",
    "command": "pasteAndIndent.action",
    "when": "editorTextFocus && !editorReadonly"
},
{
    "key": "ctrl+v",
    "command": "editor.action.clipboardPasteAction",
    "when": "!editorTextFocus"
},
{
    "key": "ctrl+shift+v",
    "command": "editor.action.clipboardPasteAction",
    "when": "editorTextFocus && !editorReadonly"
}

Solution 3 - Visual Studio-Code

Ctrl + V followed by Ctrl + Z seems to work out of the box.

Solution 4 - Visual Studio-Code

Give my paste-indent extension a try. Content is pasted according to the indentation of the current line and left justified to that indentation.

Solution 5 - Visual Studio-Code

A simple fix is to try Ctrl + Shift + V after copying the code. It will paste all codes correctly with indentations in it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTiagoView Question on Stackoverflow
Solution 1 - Visual Studio-CodeandyView Answer on Stackoverflow
Solution 2 - Visual Studio-Codehallow_meView Answer on Stackoverflow
Solution 3 - Visual Studio-CodePhilippeView Answer on Stackoverflow
Solution 4 - Visual Studio-CodeLes GrieveView Answer on Stackoverflow
Solution 5 - Visual Studio-Codevikrant guptaView Answer on Stackoverflow