October 25th, 2022
heart7 reactions

Copy with proper indentation

Principal Product Manager

You want to share some code you’ve written with a colleague, so you select it in the editor and hit Ctrl+C to copy it. As you paste it in Outlook/Slack/Teams, you realize that the indentation levels are inconsistent due to your original selection. You must now either go back to Visual Studio and do a box selection and copy that, or manually fix the indentation issue in Outlook. You know this, yet you make this mistake almost every time. Sounds familiar?

Image selection

It usually happens because you start your selection without the leading indentation. But every subsequent line has it. The result looks something like this when you paste it into Outlook:

Code with improper indentation pasted into Outlook

If this has happened to you, you’re not alone. In fact, multiple developers have reported this and requested a feature to fix it, so remember to vote for it if you agree.

Let’s do an experiment to see if we can fix this problem once and for all.

The experiment

We’ve created an extension called Copy Nice for this experiment. It removes unwanted indentation when you copy text from the editor. The question is, how does it determine if indentation is unwanted or not?

Consider this selection:

A common code selection pattern when copying code snippets

Notice how the first line of the selection doesn’t have its leading indentation selected. Also, all other lines in the selection contain only whitespace in the same span. This pattern is very common when copying code to share it with others in email or IM or elsewhere. In this case, Visual Studio recognizes the pattern and strips the leading indentation from the selection to end up with a well-formatted code snippet.

A well-formatted code snippet pasted into Outlook

For Visual Studio to correctly identify when to strip indentation, these conditions must all be true:

  1. Selection mode is not Box Selection
  2. Selection is not multi-caret selection
  3. Selection spans multiple lines
  4. Selection must start on an empty indentation

This makes sure that what you copy is always what you expect. But in case you want to disable this feature, you can of course do that from the Edit -> Advanced -> Strip Leading Whitespace on Copy command.

Toggle the feature on/off in Edit -> Advanced -> Copy Without Indentation

Next steps

If this feature is interesting to you, please install the Copy Nice extension and take it for a ride. Make sure to share any ideas and bugs on the GitHub issue tracker, and feel free to send pull requests too. The feature request ticket is a suitable place to share your comments and thoughts, so make sure to vote and comment there as well.

Do you like the idea of community experiments like this one? Let us know in the comments below.

Author

Mads Kristensen
Principal Product Manager

Mads Kristensen is a Principal Product Manager at Microsoft, working to enhance productivity and usability in Visual Studio. He’s behind popular extensions like Web Essentials and File Nesting and is active in the open-source community. A frequent speaker, Mads is dedicated to making Visual Studio the most enjoyable IDE for developers.

21 comments

Discussion is closed. Login to edit/delete existing comments.

Sort by :
  • Julien CouvreurMicrosoft employee

    Awesome. Thanks! I’d filed an issue just on this: https://github.com/dotnet/roslyn/issues/64993
    I still wonder whether this should be the default behavior when copying contents out of a raw string literal…

  • Markus Hastreiter

    I usually press ALT while selecting the text for that matter. It keeps the start column intact. What I find more annoying is that Copy&Paste does not respect the theme (colors from syntax highlighting and background color). Is there an extension for that as well?

    • Trey Morris

      There’s several available depending on your version of Visual Studio. Here’s the one from Microsoft DevLabs:

      Copy As Html – Visual Studio Marketplace

      • Richard Cox

        Note: the presence of the Copy as HTML extension will prevent this extension working.

        As I never really use the copy as HTML, this wins as being more commonly useful!

    • Mads KristensenMicrosoft employee Author

      I don’t know of such an extension

  • Andreas Plahn

    As usual Mads, small but yet so smart thinking. Thank you!

  • Akash Bagh

    The mistake lies in step one. Just select entire lines. It makes your life so much easier, everywhere. No special Visual Studio magic needed. If you’ll get used to this changed behaviour, you’ll only be unhappy when it doesn’t change in all other applications as well. I’d be much more pleased if Microsoft would finally fix the massive amount of critical bugs in the existing VS editors, most of all the broken-beyond-repair Razor editor. It messes around everything and even destroys code! Isn’t that more important than such gimmicks like this one here? I’m very disappointed by Microsoft.

    • Mads KristensenMicrosoft employee Author

      I agree copying the full line is a solution, but it has its issues.

      1. You sometimes forget to do that and must go back and redo the copying
      2. If you paste into Teams or Notepad, there is no way to easily remove all leading indentation using box selection like in Office apps

      Working on paper-cut level issues like this one is not mutually exclusive to fixing other issues in Razor or any other area. Separate teams, separate buckets of work.

      • Chuck Ryan

        It might not be, but even a good idea does not justify continuing to expand the bloated monstrosity that VS has become with trivial tweaks and the internal contest Microsoft is holding to see who can find the least useful way to add a new search.

        Please stop adding even more ways for the system to fail, as your talents would be much more valuable if applied to de-bloating the beast.