Tabs vs. Spaces "an eternal HOLY war!"
Submitted by charlie.collins on Fri, 10/19/2001 - 09:42
Tagged:
I stumbled across a great article on programmatic style and tabs v spaces while searching for some emacs customization stuff. Jamie Zawinski took the time to explain the issues and highlight the differences in editors (vi and emacs included.) This is really a great article and while it is a simple priciple the debate does get heated and many many development shops dont enforce a standard which leads to serious problems (such as lost productivity and hard to maintain code.)
Article reprinted below with zero permission and article linked. Check it out, and for the record FOUR SPACES!!!!
Tabs versus Spaces:An Eternal Holy War.
© 2000 Jamie Zawinski
<jwz@jwz.org>
The last time the tabs-versus-spaces argument flared up in my presence, I wrote this. Gasoline for the fire? Maybe. I think a big part of these interminable arguments about tabs is based on people using the same words to mean different things. In the following, I'm trying to avoid espousing my personal religion here, I just thought it would be good to try and explain the various sects. Anyway. People care (vehemently) about a few different things:
The last time the tabs-versus-spaces argument flared up in my presence, I wrote this. Gasoline for the fire? Maybe. I think a big part of these interminable arguments about tabs is based on people using the same words to mean different things. In the following, I'm trying to avoid espousing my personal religion here, I just thought it would be good to try and explain the various sects. Anyway. People care (vehemently) about a few different things:
-
When reading code, and when they're done writing new code, they
care about how many screen columns by which the code tends to
indent when a new scope (or sexpr, or whatever) opens.
When there is some random file on disk that contains
ASCII byte #9, the TAB character, they care about how
their software reacts to that byte, display-wise.
When writing code, they care about what happens when they press
the TAB key on their keyboard.
-
A lot of people like that distance to be two columns, and a lot of
people like that distance to be four columns, and a smaller number
of people like to have somewhat more complicated and context-
dependent rules than that.
-
On defaultly-configured Unix systems, and on ancient dumb terminals
and teletypes, the tradition has been for the TAB character to mean
``move to the right until the current column is a multiple of 8.''
(As it happens, this is how Navigator interprets TAB as well.)
This is also the default in the two most popular Unix editors,
emacs and vi.
In many PC and Mac editors, the default interpretation is the same,
except that multiples of 4 are used instead of multiples of 8.
However, some people configure vi to make TAB be mod-2 instead of
mod-4 (see below.)
With these three interpretations, the ASCII TAB character is
essentially being used as a compression mechanism, to make sequences
of SPACE-characters take up less room in the file.
Both Emacs and vi are customizable about the number of columns used.
Unix terminals and shell-windows are usually customizable away from
their default of 8, but sometimes not, and often it's difficult.
A third interpretation is for the ASCII TAB character to mean
``indent to the next tab stop,'' where the tab stops are set
arbitrarily: they might not necessarily be equally distanced from
each other. Most word processors can do this; Emacs can do this.
I don't think vi can do this, but I'm not sure.
On the Mac, BBedit defaults to 4-column tabs, but the tabstops
can be set anywhere. It also has ``entab'' and ``detab'' commands,
for converting from spaces to tabs and vice versa (just like
Emacs's ``M-x tabify'' and
``M-x untabify''.)
-
Some editors (like vi) treat TAB as being exactly like X, Y,
and Z: when you type it, it gets inserted into the file, end of
story. (It then gets displayed on the screen according to point
#2.)
With editors like this, the interpretation of point #2 is what
really matters: since TAB is just a self-inserting character, the
way that one changes the semantics of hitting the TAB key on the
keyboard is by changing the semantics of the display of the TAB
character.
Some editors (like Emacs) treat TAB as being a
command which
means ``indent this line.'' And by indent, it means, ``cause the
first non-whitespace character on this line to occur at column
N.''
To editors like this, it doesn't matter much what kind of
interpretation is assigned to point #2: the TAB character in a
file could be interpreted as being mod-2 columns, mod-4 columns,
or mod-8 columns. The only thing that matters is that the editor
realize which interpretation of the TAB character is being used,
so that it knows how to properly put the file characters on the
screen. The decisions of how many characters by which an
expression should be indented (point #1) and of how those columns
should be encoded in the file using the TAB character (point #2)
are completely orthogonal.
-
(setq c-basic-indent 2)
or (setq c-basic-indent 4)
-
(setq tab-width 4)
or (setq tab-width 8)
-
(setq indent-tabs-mode nil)
-
/* -*- Mode: C; tab-width: 4 -*- */
-
/* -*- mode: java; c-basic-indent: 2; indent-tabs-mode: nil -*- */
(defun java-mode-untabify ()
(save-excursion
(goto-char (point-min))
(while (re-search-forward "[ \t]+$" nil t)
(delete-region (match-beginning 0) (match-end 0)))
(goto-char (point-min))
(if (search-forward "\t" nil t)
(untabify (1- (point)) (point-max))))
nil)
(add-hook 'java-mode-hook
'(lambda ()
(make-local-variable 'write-contents-hooks)
(add-hook 'write-contents-hooks 'java-mode-untabify)))
That ensures that, even if I happened to insert a literal tab in
the file by hand (or if someone else did when editing this file
earlier), those tabs get expanded to spaces when I save. This
assumes that you never use tabs in places where they are actually
significant, like in string or character constants, but I never do
that: when it matters that it is a tab, I always use '\t'
instead.
Here are some details on vi, courtesy of Woody Thrower:
Standard vi interprets the tab key literally, but there are popular
vi-derived alternatives that are smarter, like
vim. To get vim to interpret tab
as an ``indent'' command instead of an insert-a-tab command, do this:
-
set softtabstop=2
-
set shiftwidth=2
-
set tabstop=4
-
set expandtab
-
/* ex: set tabstop=8 expandtab: */







Comments
Re: Tabs vs. Spaces
Re: Tabs vs. Spaces
RE: Tabs vs. Spaces "an eternal HOLY war!"
RE: Tabs vs. Spaces "an eternal HOLY war!"
RE: Tabs vs. Spaces "an eternal HOLY war!"