169

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.

5
  • 1
    a) do not copy the spaces of the first row from your source or b) when pasting it make sure your cursor is at the beginning of the line. Basically VSC just pasts it in there "as it is".
    – newBee
    Jan 22, 2017 at 11:25
  • 1
    So is there no way to change that through settings?
    – Tiago
    Jan 22, 2017 at 12:20
  • 2
    Highlighting the text and moving it with ALT + Arrowkey will preserve indentation. For those looking for an alternative to changing settings. Dec 1, 2017 at 1:05
  • Changing the Auto Indent setting from "Full" to "Advanced" worked for me!
    – rinogo
    May 12, 2022 at 17:55
  • For anyone wanting to see what this looks like, go do Ctrl + Shift + V in Sublime Text. It does this correctly. May 12, 2023 at 2:43

8 Answers 8

154

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

11
  • 41
    This unfortunately also formats any pasted code, which is not always what you want.
    – philraj
    Mar 28, 2018 at 22:41
  • 28
    No, it is not the same. There is a long-living issue github.com/Microsoft/vscode/issues/6392, which is closed, but actually not resolved, see comments. Everything we want is just to keep/match indentation level of the destination/target line, not the initial level, where text was copied from, while "editor.formatOnPaste": true is doing format, e.g. PEP8 fro python.
    – ololobus
    Nov 9, 2018 at 13:38
  • 4
    This is very opiniated and adds other undesirable modifications (or "beautifications") to the code.
    – dan674
    Nov 19, 2019 at 3:09
  • 7
    This is not opinionated, as much as I agree with the rest of your statement. COPY and PASTE SHOULD HAVE NO opinion, it is COPY and PASTE they should only work 1 way. Nov 27, 2019 at 3:03
  • 4
    Setting it to 'false' (deactiving it) finally resolves this annoying default behaviour. Nov 25, 2021 at 22:34
60

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"
}
8
  • 18
    Use this to recreate Sublime Text 3 behaviour ie. cmd+shift+v to paste-indent: { "key": "cmd+shift+v", "command": "pasteAndIndent.action", "when": "editorTextFocus && !editorReadonly" }
    – phocks
    May 13, 2017 at 23:10
  • 9
    Found it, go into "Keyboard Shortcuts" and then click the "Open Keyboard Shortcuts JSON" icon in the top right of the screen, hard to find I think! Aug 21, 2019 at 12:53
  • 2
    This extension has a big downside: it formats the entire file code when pasting, not only the piece being pasted! I had to uninstall.
    – sdlins
    Jan 10, 2020 at 5:26
  • 2
    You might also want to try this one. Requires no setup, just install. Inspired by the extension mentioned above, but removes some other auto-formatting issues. Dec 16, 2022 at 10:00
  • 1
    This extension is no longer working for me, any alternatives? Oct 25, 2023 at 21:15
44

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

7
  • 9
    This surprisingly works. Kinda annoying to have to do that, but it's better than nothing
    – Nate Beers
    Jan 28, 2020 at 17:23
  • 1
    Doesn't work on Mac May 24, 2020 at 13:51
  • 3
    Did you try Cmd+V and Cmd+Z ?
    – Philippe
    May 26, 2020 at 19:18
  • 2
    leave it so a Microsoft product to fail to handle ability to copy and paste ... OMG so happy I use a linux laptop ... thanks this workaround works Aug 17, 2020 at 12:28
  • 1
    For me the ctrl+z just undoes the paste (as expected, actually)
    – SFG
    Jan 6 at 16:18
9

This has been driving me mad, actually I needed to set "editor.formatOnPaste": false

0
5

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

5
  • 2
    Thanks - this works out of the box for YAMLs!
    – aoee
    Jan 24, 2023 at 18:14
  • 1
    This works, and better than the others, too. Well done, Sir! :-)
    – Pingui
    Nov 8, 2023 at 18:20
  • This extension works very well! Thanks for sharing. To make the pasting experience more streamlined, can add the following settings to keybindng.json: { "key": "cmd+v", "command": "extension.paste-indent", "when": "editorTextFocus && !editorReadonly" },
    – Honghao Z
    Dec 18, 2023 at 19:02
  • This solved it for me. Small suggestion: Give an example of what to add to keybindings.json exactly, like g3rry does. It was a bit of figuring out for me to get it right. For now, for anyone looking for it, the command to bind is extension.paste-indent
    – SFG
    Jan 6 at 16:46
  • Here is my setting added to keybindings.json: { "key": "shift+cmd+v", "command": "extension.paste-indent", "when": "editorTextFocus && !editorReadonly" } One can also call out Command Palette then search paste-indent. Click the gear icon to set a hotkey via GUI
    – Jerry T
    Jan 31 at 3:55
1

I'm using vs code 1.68.0 on ubuntu 20.04 and the following works for me.

ctrl+[+V.

But then again, its just combination of ctrl+[ and ctrl+v (if the normal ctrl+v is "right" running). Likewise, if the normal ctrl+v is "left" running then, ctrl+]+V can be used.

0

Press Shift + Alt + F in the editor on Windows/Linux machine

-8

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

3
  • This does not work, exactly the same. You try this: highlight paragraph - CTRL C, CTRL+SHFT+V, CTRL+SHFT+V. It gets ugly!!!! Nov 27, 2019 at 3:01
  • "To try" sounds strange. What exactly does that do? What if a user changed the shortcuts?
    – Nico Haase
    May 25, 2020 at 6:54
  • 2
    This will only work in sublime Text
    – Lawhatre
    Jun 11, 2022 at 9:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.