{"id":240023,"date":"2022-10-25T08:00:56","date_gmt":"2022-10-25T15:00:56","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/visualstudio\/?p=240023"},"modified":"2022-10-24T14:58:46","modified_gmt":"2022-10-24T21:58:46","slug":"copy-with-proper-indentation","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/copy-with-proper-indentation\/","title":{"rendered":"Copy with proper indentation"},"content":{"rendered":"<p>You want to share some code you\u2019ve written with a colleague, so you select it in the editor and hit <kbd>Ctrl<\/kbd>+<kbd>C<\/kbd> 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?<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/selection.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-240027\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/selection.png\" alt=\"Image selection\" width=\"566\" height=\"100\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/selection.png 566w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/selection-300x53.png 300w\" sizes=\"(max-width: 566px) 100vw, 566px\" \/><\/a><\/p>\n<p>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:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/paste-bad.png\"><img decoding=\"async\" class=\"alignnone wp-image-240025 size-full\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/paste-bad.png\" alt=\"Code with improper indentation pasted into Outlook\" width=\"570\" height=\"111\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/paste-bad.png 570w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/paste-bad-300x58.png 300w\" sizes=\"(max-width: 570px) 100vw, 570px\" \/><\/a><\/p>\n<p><span style=\"font-size: 1rem;\">If this has happened to you, you\u2019re not alone. In fact, multiple developers have reported this and <\/span><a style=\"background-color: #f7f7f9; font-size: 1rem;\" href=\"https:\/\/developercommunity.visualstudio.com\/t\/easy-copy-code-without-indentation\/1156022\">requested a feature<\/a><span style=\"font-size: 1rem;\"> to fix it, so remember to vote for it if you agree.<\/span><\/p>\n<p>Let\u2019s do an experiment to see if we can fix this problem once and for all.<\/p>\n<h2>The experiment<\/h2>\n<p>We\u2019ve created an extension called <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=MadsKristensen.CopyNice\">Copy Nice<\/a> 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?<\/p>\n<p>Consider this selection:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/selection.png\"><img decoding=\"async\" class=\"wp-image-240027 size-full\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/selection.png\" alt=\"A common code selection pattern when copying code snippets\" width=\"566\" height=\"100\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/selection.png 566w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/selection-300x53.png 300w\" sizes=\"(max-width: 566px) 100vw, 566px\" \/><\/a><\/p>\n<p>Notice how the first line of the selection doesn\u2019t 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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/paste-good.png\"><img decoding=\"async\" class=\"wp-image-240026 size-full\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/paste-good.png\" alt=\"A well-formatted code snippet pasted into Outlook\" width=\"547\" height=\"249\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/paste-good.png 547w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/paste-good-300x137.png 300w\" sizes=\"(max-width: 547px) 100vw, 547px\" \/><\/a><\/p>\n<p>For Visual Studio to correctly identify when to strip indentation, these conditions must all be true:<\/p>\n<ol>\n<li>Selection mode is not <em>Box Selection<\/em><\/li>\n<li>Selection is not multi-caret selection<\/li>\n<li>Selection spans multiple lines<\/li>\n<li>Selection must start on an empty indentation<\/li>\n<\/ol>\n<p>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 <strong>Edit -&gt; Advanced -&gt; Strip Leading Whitespace on Copy<\/strong> command.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/menu.png\"><img decoding=\"async\" class=\"wp-image-240024 size-full\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/menu.png\" alt=\"Toggle the feature on\/off in Edit -&gt; Advanced -&gt; Copy Without Indentation\" width=\"578\" height=\"329\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/menu.png 578w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2022\/10\/menu-300x171.png 300w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/a><\/p>\n<h2>Next steps<\/h2>\n<p>If this feature is interesting to you, please install the <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=MadsKristensen.CopyNice\">Copy Nice<\/a> extension and take it for a ride. Make sure to share any ideas and bugs on the <a href=\"https:\/\/github.com\/MadsKristensen\/CopyNice\/issues\">GitHub issue tracker<\/a>, and feel free to send pull requests too. The <a href=\"https:\/\/developercommunity.visualstudio.com\/t\/easy-copy-code-without-indentation\/1156022\">feature request ticket<\/a> is a suitable place to share your comments and thoughts, so make sure to vote and comment there as well.<\/p>\n<p>Do you like the idea of community experiments like this one? Let us know in the comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You want to share some code you\u2019ve 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 [&hellip;]<\/p>\n","protected":false},"author":642,"featured_media":240027,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1388,155],"tags":[6862],"class_list":["post-240023","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-extensibility","category-visual-studio","tag-experiment"],"acf":[],"blog_post_summary":"<p>You want to share some code you\u2019ve 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/240023","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/users\/642"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/comments?post=240023"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/240023\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media\/240027"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media?parent=240023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=240023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=240023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}