Possible Duplicate:
How do I format my code blocks?

When writing code in SO, I always press Tab to indent my code. It doesn't work. Instead, I lose focus on the question text area.

To actually indent, I use Spacebar, but doing this to many lines of code is annoying.

What is the right way to indent code?

marked as duplicate by Pops, jonsca, Toon Krijthe, Paŭlo Ebermann, Bo Persson Sep 30 '12 at 14:00

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.

  • Many answers has the ctrl+k shortcut. Is there a reference with all shortcuts? – Daniel Moura Jul 15 '09 at 20:22
  • 2
    Jonathan Sampson needs to make a greasmonkey script that captures tabs in question/answer textareas that adds 4 spaces and keeps the focus. – tj111 Jul 15 '09 at 20:27

Four spaces is the proper indentation for code on SO to make the markdown editor recognize it. Beyond this, the language might dictate it, but using the spacebar is really the only way.

If you find this annoying, just write your code in your favorite editor and paste it into the SO editor. From there, highlight it and press ctrl + k or click the code button in the toolbar.

  • ...the community has spoken? Four is the number to get the editor to recognize code, not necessarily the proper indentation. Unless we have a coding style guide somewhere? (that would be a good idea, if people followed it) – Andy Mikula Jul 15 '09 at 20:13
  • @Andy: Yes it is common sense that we would use the same indentation throughout. We don't need a community vote to decide on common sense. – GEOCHET Jul 15 '09 at 20:14
  • It's not the only way, you can just select the code and hit the button in the editor toolbar, right? – Jeff Yates Jul 15 '09 at 20:19
  • sigh Yes my pedantic friend. – GEOCHET Jul 15 '09 at 20:23
  • @Rich: My comment came off snarkier than intended - I just wanted to clarify the distinction between the automatic 'formatting' (i.e. syntax highlighting) and formatting as far as convention and style goes. – Andy Mikula Jul 15 '09 at 21:23

When I want to paste in a snippet of code, I usually type it up in my own text editor first, convert the tabs to spaces, then paste it into the editor here, select it, and use the code button to indent it.

I think that this is probably a better approach, too, because if you type code into the site without testing it first, it could have bugs. And if you've tested it, you've presumably written it in your own code editor, and can simply copy and paste from there.


This thought has actually caused me to install The "It's All Text!" addon for Firefox.


You can manually indent four spaces for code, which will automatically trigger the code-highlighting, or you can highlight your code and hit the code button on the editor which will automatically indent and then add the syntax highlighting for you.

  • 1
    or for the coders who haven't noticed that the batteries on their wireless mouse ran flat 2 months ago; ctrl+k – Marc Gravell Jul 15 '09 at 20:14
  • I was unaware of Ctrl+K. That will be useful. – TheTXI Jul 15 '09 at 20:18
  • 'K' for 'Kode'. – Andy Mikula Jul 15 '09 at 21:23

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