38

I've noticed that most of the HTML/XML/HAML that gets generated from plugins uses 2 spaces instead of 1 tab. I use textmate and have tabs set to 4 spaces for HAML/HTML/XML and 2 spaces for Javascript/Ruby, but I only have to press the tab key once to get nice indentation. Pressing the space bar twice and delete twice seems like too much work :p.

Do you manually type two spaces, or is some middle layer converting tabs to two spaces? Or do just a few of you use tabs?

3
  • 7
    Textmate allows the option of replacing the tab key with x number of spaces (2 or 4). I assume other text editors would allow the same. Apr 20, 2010 at 21:37
  • 1
    @Tony Fontenot is correct. Just make sure you enable "Soft Tabs".
    – maček
    Apr 20, 2010 at 21:44
  • Done! Soft tabs and tab settings are on the bottom of the textmate window for others out there.
    – Lance
    Apr 20, 2010 at 21:48

6 Answers 6

68

2 spaces is generally agreed-upon.

6
  • 4
    is there any reason behind this, for reference? thanks for the answer, I'll start doing that :)
    – Lance
    Apr 20, 2010 at 21:47
  • 6
    Well, since you asked specifically about Rails, I guess I can point to the fact that the Rails codebase itself uses 2 spaces. Since it is such a widely-used and contributed-to codebase, I think we can accept 2 spaces as convention.
    – Ben
    Apr 20, 2010 at 23:30
  • 2
    So to answer your original question. It's standard because it's convention. And it's convention because it's standard. en.wikipedia.org/wiki/Circular_reasoning
    – Gerry
    Jul 1, 2012 at 9:47
  • 3
    I know @Gerry is just being funny, but no---it's just standard because it's convention. Those who originated the convention were not following a standard.
    – Ben
    Jul 12, 2012 at 7:42
  • 2
    He wanted to know the reasoning behind the choice and your response amounted to "because it was chosen". I was poking fun at that.
    – Gerry
    Jul 12, 2012 at 8:05
37

As for all the arguments about different editors showing tabs with different widths...

It is a configuration setting people!

Change your editors to use two spaces for each tab. Different widths in different editors is not an argument. It is a moot point. If anything, using tabs allows people who prefer different widths to set and use the width they prefer. Hell, I think I like 3 spaces.

And you have to hit backspace twice just to un-indent something once. I'm a programmer, I'm lazy. I don't like to do things more than once!

4
  • With tabs you are forced to configure the width to view properly. With spaces you are forced to configure width to edit with ease (e.g. vim on backspace removes multiple spaces). Therefore for spaces there is at least some chance that you won't have to configure only to view properly. Spaces take up more... space though. Feb 1, 2014 at 12:36
  • 1
    You can configure tab width once in your editor and never have to change it again. That's only if you don't want the default. Which on every modern IDE/editor I've used, defaults to 4.
    – Mike
    Feb 3, 2014 at 15:20
  • 4
    People always say they prefer spaces because code looks the same everywhere, as if that was desirable to everyone. The idea of using tabs is that people can see the code the way they please. If your code looks bad when someone changes tab size then it's your fault, not the editor's. Code should look good regardless of my choice of tab size for my editor/viewer (given that I choose a sane size, say between 2 and 8). People should stop forcing everyone else to use their preference of indentation, and that's what tabs give you.
    – Jens
    Dec 9, 2014 at 1:44
  • 2
    I would like this chiseled into my tombstone. Well said, Jens.
    – Mike
    Dec 9, 2014 at 14:07
6

2 spaces. Even for the plain Ruby kids.

4

Two spaces are emerging as the standard because they are “just enough” indentation to make the HTML clearly indented to most people's eyes, but because HTML tends to nest very deeply — much more deeply than one would commonly nest when programming — and anything more than two spaces tends to start pushing HTML off the right edge of an 80-column screen pretty quickly.

Do not use tabs; use two spaces. Tabs are problematic because they can be a different width in different editors (and terminals, for that matter). Since whitespace is removed when you tidy your HTML before deployment, there is no ultimate bandwidth benefit to using tabs; they simply introduce a wildcard into your development experience.

I use Emacs, which auto-indents XML, HTML, and SGML with two spaces. I would be quite annoyed if I had to type indentation characters by myself; that's why I have an editor. :)

1

2 spaces is the convention for Ruby which Ruby on Rails is based upon.

1

In more practical terms, if you use mutliple OS and editors ( such as at work I used RubyMine on Win XP and TextMate on my MacBook at home and one of our developers uses Notepad ++ and another Aptana and we even have one who uses Netbeans, all of these editors interpret tabs differently - different indentation, but 2 spaces are always 2 spaces. Also if you ever use HAML or if you need to copy paste your code into some websites that try to colorcode your code, 2 spaces are essential.

3
  • This article talks about indenting on your screen - I'm talking about sharing code and having the best consistency, you can't expect other people to adjust their tab settings everytime they open your code.
    – konung
    Jul 25, 2012 at 20:27
  • 1
    I believe you might be confusing indentation level with alignment. Here is a deeper explanation: movementarian.org/docs/whytabs
    – Gerry
    Jul 26, 2012 at 19:54
  • No I don't - in a sense that a lot of editors rely on indentation in code, to align it visually on the screen ( which also depends on font used - some kind of monospaced font is best usually - but that's personal preference).
    – konung
    Oct 27, 2012 at 1:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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