Quite often I see code formatted like this in questions:


If the code consists of sufficiently long lines, it ends up looking like this:


It'd be nice if there was a way (an icon to click or a magic keyboard incantation perhaps) to correct those two code blocks to:





Presumably this could work by looking at the lowest number of spaces x in the selected block, and then reducing the number of initial spaces on each line by x-4.

Obviously, there are a small minority of languages where spaces are meaningful that this might change the meaning of, but in most cases I think this would be useful.

I'm aware that I can cut and paste the code into my editor of choice to do this, but it'd be nice if there was some built-in way to do this.


3 Answers 3


There's two big issues with code formatting that leads to side-scrollitis:

  1. excessive intendation
  2. excessively long lines

#1 can be solved by removing excessive indentation or by converting 8-space tabs to 4 spaces.
#2 can be solved by inserting more line breaks, e.g. turning

MyFunction(reallyreallylongarg1, reallyreallylongarg2, reallyreallylongarg3, reallyreallylongarg4, reallyreallylongarg5, reallyreallylongarg6);


MyFunction(reallyreallylongarg1, reallyreallylongarg2,
    reallyreallylongarg3, reallyreallylongarg4,
    reallyreallylongarg5, reallyreallylongarg6);

#1 could possibly be done programmatically by the site, but #2 depends on the syntax of the language and is best done by a human who can decide the best reformatting based on context. But we've got lots of users with editing privileges; does this really need to be addressed programmatically?

tl;dr: This is what 2000+ reputation users are for! If you see badly formatted code, step in and clean it up! All hail the Junior Janitor!

  • 2
    Yes there are lots of users with editing privileges (on SO I'm one of them), and I for one want to fix code that's hard to read - a good deal of which fits into #1, and is tedious to fix by hand. A programmatic fix would make it easier to be a good SO citizen, that's all I'm saying. Jan 14, 2010 at 18:47

You can use Ctrl+K repeatedly to delete all except the last N % 4 spaces.

  • Not in the version of WMD that Meta is currently using - is that the latest one? Feb 11, 2010 at 9:23
  • @Dominic: It Works-For-Me[tm] on SO and here. I'm using Chrome. Feb 16, 2010 at 11:58
  • Ah - I see what it does. Doesn't quite do what I want, because it doesn't end up as just 4 spaces - as you point out, so I'd still have to edit each line. Feb 16, 2010 at 12:23
  • @Dominic: after looking at the WMD source, I came around to see that one to three spaces in front of the source are acceptable :-) Feb 18, 2010 at 12:03

It's open source! You can add that feature yourself!



You must log in to answer this question.

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