2

Are there any good ways to copy code from Visual Studio that keeps the code neat looking? I have two main concerns that are large enough for me to raise this question:

  1. When copying multiple lines of code from a nested code block, the leading whitespace on every line should be removed. This one is easy to fix manually though, by simply using shift + tab until it aligns as it should. Still something I'd like to avoid though.
  2. More importantly, however, is when copying from code where the setting "replace with tabs" is used, the indetions will be huge when copied to e.g. Notepad, making the code practically unreadable.

I'm hoping for a one-click solution here, be it an Add In or Macro, that does not matter, as long as I can select a block of code, and then copy it with one click, and have it look well when I paste it somewhere else.

3
  • Have you tried pasting the code into something else? For example Notepad++? – Apache Jul 2 '12 at 22:35
  • @Shiki: I thought that was quite clear from my question. :) Yes, and my problem is mainly spacing, which I what I would like to control, or at least have a better default. – Johny Skovdal Jul 3 '12 at 6:29
  • Oh. Okay, sorry. Uhm... what about some kind of "Clipboard control"? Like writing a small C# application, that will run in the tray, and use that with a hotkey. So when you copy from VS, you get the format you want. .... hmmm.. maybe it's an overkill, but hey, better than nothing! – Apache Jul 3 '12 at 10:54
3

The Visual Studio Extension Copy for review may help you out

It currently supports two output formats, both removing leading whitespace, while keeping the indentation as much as possible.

Example: The following code (verbatim copy)

        foreach (OrderByInfo orderByInfo in ParseOrderBy(orderBy)) {
            collection = ApplyOrderBy<T>(collection, orderByInfo);
        }

will get formatted to (using the Stack Overflow output format):

8<---------------------------------------------

Here's my code (in OrderByHelper.cs):

foreach (OrderByInfo orderByInfo in ParseOrderBy(orderBy)) {
    collection = ApplyOrderBy<T>(collection, orderByInfo);
}

8<---------------------------------------------

However, syntax highlighting does only work on the stackoverflow.com site, not here on superuser.com. It's a feature of the site, not the extension.

Get the extension from the Visual Studio Gallery.

Disclosure: I am the author of that Extension. Please notify me if I can improve it to your needs.

2
  • Thanks, I'm very impressed with that solution so far! The only thing missing, as I see it, is a copy option that includes the code markup for syntax highlighting here on StackOverflow. :) – Johny Skovdal Mar 20 '15 at 6:48
  • 1
    @JohnySkovdal Syntax highlighting is not supported here on Super User. I have added that to the answer, plus made an example. – Marcel Mar 20 '15 at 8:13
0

If HTML output is okay, there is an extension called Copy as HTML.

http://visualstudiogallery.msdn.microsoft.com/98fef791-eb65-4cdf-bf84-077b98c234cf/

1
  • For most scenarios that won't work no, but thank you for pointing this one out though, it certainly can come in handy. :) – Johny Skovdal Jul 3 '12 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.