160

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.

Tiago
  • 4,233
  • 13
  • 46
  • 70
  • 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 '17 at 11:25
  • So is there no way to change that through settings? – Tiago Jan 22 '17 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. – Daniel Thompson Dec 01 '17 at 01:05
  • Changing the `Auto Indent` setting from "Full" to "Advanced" worked for me! – rinogo May 12 '22 at 17:55
  • For anyone wanting to see what this looks like, go do `Ctrl` + `Shift` + `V` in Sublime Text. It does this correctly. – Gabriel Staples May 12 '23 at 02:43

7 Answers7

146

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

andy
  • 2,362
  • 2
  • 18
  • 19
  • 33
    This unfortunately also formats any pasted code, which is not always what you want. – philraj Mar 28 '18 at 22:41
  • 21
    No, it is not the same. There is a long-living issue https://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 09 '18 at 13:38
  • Well it works how I want it to. I want it to past the section i copied to wherever the cursor is when I paste – Dave Pile Apr 13 '19 at 09:37
  • 4
    This is very opiniated and adds other undesirable modifications (or "beautifications") to the code. – dan674 Nov 19 '19 at 03:09
  • I don't want to change the format of my text when I paste I want it to paste exactly what I have copied, if I copy it wrong it is my fault, This seems like an incorrect answer. Although seems well received so I want to try it :) – TheArchitecta Nov 27 '19 at 02:54
  • 2
    Every Text Editor except VSCode allows a User to triple click and highlight any text. CTRL C to copy and then CTRL V, CTRL V to duplicate the copied text. Except in VS Code a triple Click CTRL C CTRL V adjusts the text and then the 2nd CTRL V makes it even worse. CTRL C CTRL V is the most common of keyboard shortcuts, even my Mum knows this one. – TheArchitecta Nov 27 '19 at 02:57
  • 6
    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. – TheArchitecta Nov 27 '19 at 03:03
  • No, this does not work for Python at the very least (indentation has meaning) – Cornelius Roemer May 24 '20 at 14:03
  • I agree with @philraj – Blues Clues May 12 '21 at 07:12
  • 1
    Setting it to 'false' (deactiving it) finally resolves this annoying default behaviour. – Christian Bonato Nov 25 '21 at 22:34
  • This also doesn't work if the formatter doesn't support formatting pasted sections. – Sriram Nov 28 '21 at 14:00
  • It's not exactly the answer for the question, it will format everything instead of just keeping the correct indentation on for the paste, also will require python package that will perform formatting. – Vedmant Mar 29 '22 at 08:09
  • @philraj Yep, I had to disable it because it kept yielding obnoxious results by removing all the blank space when I cut and paste multiple lines with empty lines. – Chiramisu Sep 01 '23 at 19:55
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"
}
Our_Benefactors
  • 3,220
  • 3
  • 21
  • 27
hallow_me
  • 1,203
  • 10
  • 16
  • 17
    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 '17 at 23:10
  • This solution still works. No more nasty extra tab vs code likes to put in every time I copy/paste . – Luminous Nov 19 '18 at 14:42
  • 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! – Matt Fletcher Aug 21 '19 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 '20 at 05:26
  • To locate the `keybindings.json` where you need to add the JSON snippet: Search for "Preferences: Open Keyboard Shortcuts (JSON) in Command Palette – Cornelius Roemer May 24 '20 at 14:02
  • 1
    You might also want to try this [one](https://marketplace.visualstudio.com/items?itemName=gazugafan.vscode-indent-on-paste). Requires no setup, just install. Inspired by the extension mentioned above, but removes some other auto-formatting issues. – infinite789 Dec 16 '22 at 10:00
36

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

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Philippe
  • 6,703
  • 3
  • 30
  • 50
5

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

Pixelomo
  • 6,373
  • 4
  • 47
  • 57
2

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

Les Grieve
  • 708
  • 8
  • 10
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.

Lawhatre
  • 1,302
  • 2
  • 10
  • 28
-7

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

Nimantha
  • 6,405
  • 6
  • 28
  • 69