What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 134 Stack Exchange communities.

Often I see code blocks pasted into questions with no formatting, or terrible formatting.

It's sometimes quite a chore to fix these: you have to either manually space out each line, or paste the code into an IDE, format it there, and paste it back.

Could we have a button/function where you could highlight a code block, and it would turn this:

foreach (var i in stuff) {
if (i==1) {
Whatever();
}
}

into this:

foreach (var i in stuff) {
  if (i==1) {
    Whatever();
  }
}

I realise the function would have to work differently based on the language in the code block. Perhaps it could use the same rules as the prettify code.

Also, it wouldn't be a big deal if it gets the language wrong and breaks the code, since you can always just ctrl+z.

share|improve this question
1  
I'd favor smaller code fragments (which, I feel, often cannot be indented automatically as they are incomplete by definition, and hence often are unbalanced) over folks pasting lots of code... –  Arjan Mar 11 '11 at 10:31
3  
From my POV it's more for fixing other people's code - my code is already indented when I paste it anyway. Good point about fragments, although I think I'd expect most fragments to be balanced.. –  Blorgbeard Mar 11 '11 at 10:35
1  
Just some fuel for thought: The formatting of code can also be an error source, so reformatting the code could fix this possible problem (especially with languages like Python) and would leave the audience and the OP completely baffled, without being visible on first sight why the error is not reproducible anymore. –  Time Traveling Bobby May 2 '11 at 11:26

2 Answers 2

That would take a fair bit of work to implement, but simple Increase Indent and Decrease Indent buttons would make reformatting code MUCH easier.

share|improve this answer
    
Just for the archives: text that is indented can be outdented by selecting all lines, and hitting the code button or its Ctrl-K keyboard shortcut. Indenting kind of works the same: the button/Ctrl-K will indent selected text if it's not indented yet. Of course, this does NOT allow one to indent partial text multiple times (as after indenting, the button/Ctrl-K will outdent, rather than indent more), so the current behaviour is not exactly what is suggested in the above answer. –  Arjan Mar 11 '11 at 15:23
    
@Arjan wat. o the current behaviour is not exactly what is suggested in the above answer I didn't speak of any current behavior. What you described decreases indents until none are left, then reverts to an indent/remove indent behavior. You cannot directly control increasing or decreasing indents, which is a feature that I'm talking about. –  Won't Mar 11 '11 at 15:32
    
I meant the current behaviour that I described, when using Ctrl-K et al. :-) (And that the things I described are not what you suggested. So, my comment was just to explain the limited current possibilities, for those who did not know about that...) –  Arjan Mar 11 '11 at 15:40
    
(And yes, I like your suggestion!) –  Arjan Mar 11 '11 at 15:42

if you have long blocks of code to indent I have found a trick to help with that:

when writing code after the first indent (to make the code block) you have this:

    foreach (var i in stuff) {
    if (i==1) {
    Whatever();
    }
    }

after the block you want to indent insert a new line with a single character on it

    foreach (var i in stuff) {
    if (i==1) {
    Whatever();
    }
k
    }

then select the char plus the entire block (I use shift+up for this) you want to indent and hit ctrl+k

    foreach (var i in stuff) {
        if (i==1) {
        Whatever();
        }
    k
    }

then delete the inserted line

    foreach (var i in stuff) {
        if (i==1) {
        Whatever();
        }
    }

that's how I quickly indent large pieces of code

then again an increase-indent button would help

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .