hard-tabs soft-tabs 区别

作者 甘言川
2016.10.09 20:42* 字数 421





Tabs vs. Spaces

FRI • MAY 28, 2010490

First off, quick definition: I am referring to the difference between using Tab Characters and Space Characters. Many editors support using the Tab key to insert either a tab character or a certain number of spaces.

Tab Character: Created by using the Tab key, and inserts a single character whose length depends on the settings of the code editor. Its length can vary from editor to editor.

Space Character:Created by using either the Space bar, or the Tab key with Soft Tabs enabled. One character per space, and is a set length. It will always be equal to one character in length.

Soft Tabs:Soft Tabs are created by using the Tab Key, but in reality only insert a set number of Space Characters instead. So a Soft Tab with a setting of 4 will insert Four space characters instead of one tab character when you press the Tab key.

Lets take a quick look at two images. The first shows a PHP function in an editor that has been configured to use Hard Tabs (single character per tab), equal to 4 spaces:

Now, lets look at the same file when opened on a computer/editor that has the preference set that a Hard Tab should display as two characters instead of four.Nothing lines up anymore!:

Here are some reasons for using Spaces (Specifically 2 space indents) instead of Tabs:

Consistentcode viewing on any platform: web, desktop or print

Code takes up less horizontal space, allowing more to be read at one time

It makes code easy to copy and paste for online discussion and sharing since most online viewers render Tab Characters as 4 spaces or more.

One closing thought:This is monospaced code, not MS Word. Variable width tab characters have no place here. Type what you mean, and mean it. Flexibility here only brings confusion.

TextMate:In TextMate I have it set to 2 space tabs, and Soft Tabs on (which tells TM to use spaces instead of real tab characters). This drop down is in the footer in each TextMate window: