This question already has an answer here:

I was wondering if there is an easy way to indent the code in Stack Overflow. Whenever I paste in code, all the code loses its indentation; what I have been doing is manually indenting every line with the space button. Is there a quicker way to do all of this?

marked as duplicate by ale, CRABOLO, Shadow The Dragon Wizard discussion Jan 18 '15 at 15:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from stackoverflow.com May 8 '11 at 22:52

This question came from our site for professional and enthusiast programmers.

  • 3
    Highlight it and hit the "code" button. This belongs on meta. – BlueRaja May 8 '11 at 22:49
  • You could always make a short program in your language of choice to accept text and replace all tabs with four spaces, but that might be a bit overkill. – Maxpm May 8 '11 at 22:50
  • Hardly seems worth moving it - use the "code" button and move on. – duffymo May 8 '11 at 22:51

To properly format code you should indent your code in the markdown source, either by a tab character or by 4 spaces.

There is handy button on the toolbar which can do this automatically for you, simply select the code section of your question and press this button (or type it's key shortcut Ctrl + K):

enter image description here

  • 1
    It would be nice if there was a handy button that added indention for code in lists. – NetMage Oct 31 '17 at 15:52

It would be nice if SO could automatically indent new lines to the same level as the previous line, like most text editors and IDEs do. This would make typing up answers that include code a lot easier.


Fixing indentation would be the minimum what I would expect from StackOverflow (by fixing indentation I understand removing unncessary spaces as well as adding them when necessary, for example on if/for/while loops, nested XML tags etc.).

I'd expect also auto-breaking too long lines, formatting tag hierarchy for XML etc. It shouldn't be so hard to implement in JavaScript. Yes, you can write your own browser/greasemonkey plugin. No, you can't always use browser with such tools installed (workspace, for example, where you are dependent on what you become).

Of course, such features would require choosing the source language of code block - this is another feature that is missing. You can learn by heart language: lang blocks, but it should be a menu option.

Of course, there are esoteric languages where space is a part of syntax, not formatting (Python AFAIK) so such feature must be optional and activated only by taskbar button.


You could paste you code into nano in a Unix terminal, say you've got this:

enter image description here

Save it as sad.txt in nano, then run the following line of code:

enter image description here

Here, we're replacing, as it were, the start of each line with four spaces, so when you open happy.txt you'll be happy because you'll get the most amazing result:

enter image description here

From here just paste this directly into your post. Hope this helps/makes sense.

  • 3
    That's a long and complex way to do something that can be done by clicking one single icon in the editor. – Shadow The Dragon Wizard Feb 10 '14 at 8:02