36

In Sublime text cmd+shift+v will paste and indent the code. Can this be done in visual studio code?

Workaround

I've made an extension that will let you paste and format with cmd/ctrl+shift+v. Search for pasteandformat

https://marketplace.visualstudio.com/items?itemName=spoeken.pasteandformat

Spoeken
  • 2,549
  • 2
  • 28
  • 40
  • If I properly understand you need [this](http://stackoverflow.com/questions/5755942/how-do-you-auto-format-code-in-visual-studio) – Bogdan Bogdanov Jan 13 '16 at 10:26
  • 1
    If you're feeling ambitious, you could start learning about developing [vscode extensions](https://code.visualstudio.com/docs/extensions/overview). I am just starting to get into this, so I can't give you a complete roadmap. – Eric Lease Jan 21 '16 at 07:34
  • 2
    The [`go` language extension](https://github.com/microsoft/vscode-go) would be a good reference. In particular, the [formatting lib](https://github.com/Microsoft/vscode-go/blob/master/src/goFormat.ts) might show you how you might implement the formatting functionality for the desired language, and then you would need to figure out how to trigger that by attaching to the paste event (if that's possible). But by the time you learn/implement that it might be out-of-the-box functionality in your desired language. – Eric Lease Jan 21 '16 at 07:34
  • 1
    @EricLease I'm a fast learner ;) https://github.com/spoeken/pasteandformat Took use of some buildt in commands instead of writing a formatter from bottom up. – Spoeken Jan 22 '16 at 12:16
  • @Spoeken great extension! I didn't want paste and format but only indent after paste, so I have coded an extension if you wanna try it https://marketplace.visualstudio.com/items?itemName=Rubymaniac.vscode-paste-and-indent most of the time it works.. – Gerry Oct 19 '16 at 11:43

2 Answers2

26

Currently, Visual Studio Code doesn't provide this specific functionality. We could vote for this feature at Visual Studio's UserVoice website.

There's already a ticket open for this feature: Paste and auto align code. If you've got an account, you can vote for this feature so it gets more attention. If it has enough attention, Visual Studio Code's developers could take notice of this and maybe develop it.

Current workaround

After pasting the code, You could use CTRL+E, CTRL+D for windows or ALT+SHIFT+F for mac.

But note that this will reformat the whole document, indenting according to the available rules for the source type.

If you only want this to be applied to the pasted code, select the code after pasting and then use CTRL+E, CTRL+D for windows or ALT+SHIFT+F for mac. Now the indenting/formatting is only applied to the pasted lines.

MHX
  • 1,581
  • 2
  • 21
  • 31
Dbuggy
  • 901
  • 8
  • 16
  • 2
    What (I'm) we are looking for is this functionality, but directly applied to the 'copied' text, without affecting the rest of the document. – MHX Jan 13 '16 at 10:37
  • 1
    I'm currently unaware of functionality in VS which provides the behaviour only on the pasted part. – Dbuggy Jan 13 '16 at 10:39
  • I suggested an edit on your answer, that this feature currently isn't available and how users could vote for this feature in Visual Studio's UserVoice website: https://visualstudio.uservoice.com/forums/293070-visual-studio-code/suggestions/8626678-paste-and-auto-align-code. I also improved your 'fix' a little bit. If this edit gets approved, I'll be happy to grant you the well-deserved bounty. – MHX Jan 20 '16 at 08:40
  • 1
    You improved the answer quite a lot. So i thank you kind sir. – Dbuggy Jan 20 '16 at 09:21
  • I just added an explanation of how to edit your key bindings file. But I could not find a way to add two commands for one shortcut. Anyone know if this is possible? – Spoeken Jan 21 '16 at 13:08
  • 2
    @mhx I've made an extension that will let you paste and format with `cmd/ctrl+shift+v`. Search for `pasteandformat` Note that it will format the whole document, not just what you pasted in. – Spoeken Jan 21 '16 at 15:03
  • 1
    @mhx Updated it. So now it only formats the pasted text :) If you find any issues you can report it at https://github.com/spoeken/pasteandformat – Spoeken Jan 22 '16 at 12:12
  • 1
    @spoeken awesome! Nice update... definitely will be using this for now. :-) – MHX Jan 22 '16 at 12:37
  • Looks like it's being implemented. See this UserVoice item: https://visualstudio.uservoice.com/forums/293070-visual-studio-code/suggestions/7752552-auto-indent-code-format – Fan Li May 22 '16 at 14:34
  • @martypdx yes there is, maybe it only says option on yours. – Spoeken Oct 21 '16 at 10:00
18

Since version 1.9.0, editor.formatOnPaste landed in VSCode.

Open settings.json via Code -> Preferences - Settings and search for formatOnPaste

// Controls if the editor should automatically format the pasted content. A formatter must be available and the formatter should be able to format a range in a document.

"editor.formatOnPaste": true,

Modify false to true since the default value is false

Source: Changelog update 1.9.0

MHX
  • 1,581
  • 2
  • 21
  • 31