vscode icon indicating copy to clipboard operation
vscode copied to clipboard

Copy/Cut then Paste doesn't preserve or fix indentation

Open alx-so opened this issue 10 years ago • 30 comments

  • VSCode Version: 1.1.0
  • OS Version: Windows 10 Enterprise

Steps to Reproduce:

  1. Copy some divs code in html (with any method "Ctrl + C || Right Click")
  2. Then paste inside other divs

Always have to fix intentation after pasting some code.

Experiencing the same behaviour for css and js.

alx-so avatar May 16 '16 08:05 alx-so

Have you tried to copy/paste it from/at the start of the line? div_html

wxkin avatar May 16 '16 17:05 wxkin

@wxkin I think it would be inconvenient to always target beggining of the line as you add new, hitting the "enter".

alx-so avatar May 17 '16 11:05 alx-so

This sounds like you would want some sort of format on paste.

alexdima avatar May 23 '16 19:05 alexdima

Having to fix the indentation characters after copy/pasting is really annoying. When you forget it, you end up with a file with mixed indentation. Other editors don't have this problem.

This sounds like you would want some sort of format on paste.

Applying the file's current indentation format to pasted contents would be great.

Instagit avatar Sep 15 '16 15:09 Instagit

i need this feature too, just like the "paste and indent" function in sublime with ctrl + cmd + v

alanwei0 avatar Sep 26 '16 07:09 alanwei0

This is a very basic feature to have. Is there any specific reason this hasn't been implemented yet?

rubymaniac avatar Oct 13 '16 18:10 rubymaniac

@rubymaniac The indentation rules we have at our disposal are fuzzy and there are a plethora of languages where they're incorrectly defined. e.g.

if (true)
    return 5; // <-- you probably would like this line to be indented

switch (a) {
    case 5:
        return 5; // <- same here
     default:
        return 6; // <- same here
}

You will find that these cases do not work correctly in vscode today when pressing Enter and there are probably many languages besides js that have this problem.

Now imagine pasting in proper indented code and the editor indenting it in a wrong way. You would be much more upset than the lack of the auto-indent. That's why I try to take the discussion towards format on paste, as formatting is something that can be provided by a language server, and is not language agnostic (like the indentation rules).

alexdima avatar Oct 24 '16 17:10 alexdima

@alexandrudima yes I completely agree with what you say about the fuzziness of the indentation rules. However I usually find myself to copy correctly indented code from one place to another with a different offset and VSCode is not adjusting the indentation to the new offset and I always have to do this manually.

For that reason I made a little extension that tries to adjust the offset of the indentation to where the cursor is when you are pasting code.

rubymaniac avatar Oct 25 '16 08:10 rubymaniac

Aha, so you would expect that the pasted code is treated as one blob and indented/unindented as a single blob (all lines the same amount). Makes sense to me.

That would reduce the risk of messing it up, although we'd still need to do per-line adjustments (i.e. detect that the pasted code is indented with spaces@2 meanwhile the file is indented with tabs, etc.).

alexdima avatar Oct 25 '16 08:10 alexdima

@alexandrudima Exactly what I am saying. The per-line adjustments are crucial in order to do it properly. For what it's worth in the extension I wrote I am making per-line adjustments.

Although the problem is solved (at most cases) through the extension, I feel that this should be native functionality (or at least configurable through preferences).

rubymaniac avatar Oct 25 '16 20:10 rubymaniac

No news?

It is a core functionality to be able to copy/paste without indentation surprises.

VSCode is awesome but I may go back Atom just because of that. I loose time fixing indentation after a copy/paste...

MacKentoch avatar Jan 17 '17 06:01 MacKentoch

@rebornix Has just implemented editor.formatOnPaste, so if you are in a language that supports format, the format will be invoked on the pasted content.

alexdima avatar Jan 21 '17 13:01 alexdima

@alexandrudima To be honest, editor.formatOnPaste is not a good replacement for the requested functionality. I'll try to provide some examples as I see them, but I had to turn it off because it was ruining much of my code.

martin-sweeny avatar Feb 22 '17 16:02 martin-sweeny

I'm using Python and I turned on formatOnPaste but that runs autopep8 and I don't want my code wrapped, just indented to fit the paste location. [That's a thing I miss since switching over from Atom.]

adrientetar avatar Apr 22 '17 09:04 adrientetar

last night i dreamt that this worked

ZeroVa avatar Jun 04 '17 12:06 ZeroVa

@ZeroVa You really have a sweet dream. 😆

hustcer avatar Jun 04 '17 13:06 hustcer

This is another really seemingly minor thing that should just work in VS Code. May also revert to Atom. Death by a 1000 paper cuts with VS Code as much as I want to like it.

MatrixV01 avatar Jun 05 '17 19:06 MatrixV01

Already added this feature in Insider.

rebornix avatar Jun 26 '17 18:06 rebornix

I find a extension might do this trick: pase-and-indent extension I changed ctrl+shift+v of its example configure binds into alt+z, and alt+z works.

Skura23 avatar Jul 31 '17 09:07 Skura23

Should pasting without horrible indentation be working in 1.16.0 ?

neerolyte avatar Sep 12 '17 00:09 neerolyte