Using Tabulator for Indentation

Yesterday I run into an issue that a user wants to use tabulator character for indentation level. Because I answer similar question often, I will try to explain it here. 

By default NetBeans editors use for indentation level 4 spaces and don't use tabulator character (\t). When you press Tab key, then NetBeans place caret on the column that corresponds to the next indentation level and the difference is filled with spaces. So by default pressing Tab key adds 4 spaces.  

If you want to use tabulator, you have to change the default behavior in Tools->Options->Editor->Formatting Tab. See the picture below, where are the default values.

Notice that by default the property Tab Size is setup to 8. This is because the standard size of tabulator is 8 characters wide. So when you want to use tabulator for every indent level you have to uncheck Expands Tabs to Spaces check box and you have to decrease the Tab Size property to 4 or increase Number of Spaces per Indent to 8. In other words the Tabs Size and Number of Spaces per Indent has to be the same.

I have to agree that from this point of view, the default values doesn't seem to be logical, but the terminals and many printers treat tabulator as 8 characters wide.


Comments:

One question: When I press tab, I get 4 spaces by default. What "Number of Spaces per Indent" stands for? When are 8 spaces applied with default settings?

Posted by umpirsky on May 24, 2011 at 02:49 AM CEST #

What I find illogical is not the default settings but that even after you uncheck "Expands Tabs to Spaces" you still have to reduce the number of spaces for the editor to use tabs instead of spaces for indentation. There should be no further configuration necessary apart from unchecking the box.

Posted by xerxes on May 24, 2011 at 04:46 AM CEST #

@umpirsky: IMHO there are two things. The size of indentation level "Number of Spaces per Indent" and how many space will be treat as tabulator character if the Expand Tabs to Spaces is unchecked, the property Tab Size. Because the key TAB is mapped (internally) in editor to increase indentation level, which is by default 4 spaces, the pressing TAB key generate 4 spaces. So lets say that you have unchecked the Expand Tabs to Spaces and the caret is on column 0. Then you press 3 times TAB key. It will generate 12 spaces, but the first 8 is converted to the '\t' char. So the document now have on the line '\t' and 4 spaces. If you open a document with '\t' then the every '\t' char is rendered as 8 spaces. This is what I think.

Posted by Petr Pisl on May 24, 2011 at 04:50 AM CEST #

@xerxes: Basically I agree with you. There is an issue that is saying that the default value for Tab Size property should be 4. This issue was even fixed, for one release, but then the value was returned to 8 due to the compatibility with terminals and printers, where char '\t' represents 8 characters wide.

Posted by Petr Pisl on May 24, 2011 at 05:02 AM CEST #

@Petr Thanks for the answer.

Posted by umpirsky on May 24, 2011 at 05:04 AM CEST #

I think you should change the default to have the expand tabs to spaces be off. It's much better to insert \t characters and then anyone with any editor can just set that value to be however wide they want. (There are a lot of people that like tabs to be 2 or 3 spaces wide, and there's nothing that they can do to change 4 space characters to be anything other than 4 space characters). As it stands now, the first thing I do when I get a new version of NetBeans is go to options and turn this off.

Posted by vb on May 27, 2011 at 09:55 AM CEST #

@xerxes This is because the document already contains 4 spaces for indentation. They do not change existing documents to change all space indentations to be tab indentations. This would be a nice feature to have (or at least it should ask if you want to change spaces to tabs). I know that for Visual Studio, MS provides a plugin that does exactly this. It detects that you have both spaces and tabs for indentation in one document and asks if you would like to change them all to tabs or all to spaces. I would love a similar feature in Netbeans.

Posted by vb on May 27, 2011 at 09:58 AM CEST #

If an existing document uses an 8 space tab and Netbeans is configured for a 4 space tab, then using Source->Format should fix that within the file. I do this as a matter of routine on all third party PHP code I work on to ensure everything has consistent formatting.

Posted by Guardian on June 03, 2011 at 11:02 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

This blogs is written by NetBeans developers who contribute to the PHP support mainly.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today