An Oracle blog about NetBeans for PHP

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.

Join the discussion

Comments ( 8 )
  • umpirsky Tuesday, May 24, 2011
    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?
  • xerxes Tuesday, May 24, 2011
    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.
  • Petr Pisl Tuesday, May 24, 2011
    @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.
  • Petr Pisl Tuesday, May 24, 2011
    @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.
  • umpirsky Tuesday, May 24, 2011
    @Petr Thanks for the answer.
  • vb Friday, May 27, 2011
    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.
  • vb Friday, May 27, 2011
    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.
  • Guardian Friday, June 3, 2011
    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.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.