tags:

views:

93

answers:

2

I need to be able to set the tab settings for the following file types:

  • .rb: 2 soft spaces
  • .css, .html.erb: 4 space tabs

I have tried the following, but none of it seems to alter my default tab settings for each of the file types.

;; js-mode-hook has also been tried
(add-hook 'javascript-mode-hook 
      '(lambda() 
        (setq tab-width 4)))

(add-hook 'css-mode-hook 
      '(lambda() 
        (setq tab-width 4)))

(add-hook 'html-mode-hook 
      '(lambda() 
        (setq tab-width 8)))

I am pretty new to emacs so my knowledge of configuration is pretty low.

+5  A: 

Theres a number of aspects to how Emacs does indentation. Setting the tab-width only specifics how big a tab is if a literal tab is inserted. If you don't wish to use literal tabs for indentation, then you should first disable their insertion (from the manual ):

Emacs normally uses both tabs and spaces to indent lines. If you prefer, all indentation can be made from spaces only. To request this, set indent-tabs-mode to nil. This is a per-buffer variable, so altering the variable affects only the current buffer, but there is a default value which you can change as well.which you can change as well.

However, to specify the indentation levels, you'll also need to set the c-basic-offset value variable as well:

(add-hook 'html-mode-hook 
      '(lambda() 
        (setq c-basic-offset 4)
        (setq indent-tabs-mode nil))

In your case, you may only need the c-basic-offset but try a few combinations and see what works best.

cristobalito
I using the c-basic-offset I tried with and without the indent-tabs-mode as well as with/without tab-width and none of them worked. I am using the emacs starter kit so is it possible that some other .el file is overriding my setting in chris.el?
chris
After removing all the other config files except my custom file with only the setting you have above, I still get an indentation of 2 spaces on tabbing.
chris
I believe VitoshKa's answer is more relevant in this case - what I describe should work for any cc-mode derived mode (e.g. java-mode, csharp-mode, etc.). Sorry if that's been misleading - my apologies.
cristobalito
+2  A: 

In emacs each mode has it's own indentation style. The main command to indent (bound to TAB) is indent-for-tab-command. This command calls mode specific indentation function found in the variable indent-line-function. So each mode has it's own way of doing it.

For Ruby (for my emacs 2 is a default):

 (setq ruby-indent-level 2)

For CSS (again, default is 4 for me):

 (setq css-indent-offset 4)

Unfortunately SGML mode (on which HTML mode is based) has a very simple indentation mechanism and apparently the level is not configurable. See the source code of sgml-calculate-indent function.

I personally find it weird. I am not writing HTML, but you can try to modify the sgml-calculate-indent function yourself :). Learn some lisp.

I am using js2 mode, and it indents perfectly by default. For js you have to search for js-indent-level or something similar.

Cheers.

VitoshKa
With that I get the proper tab widths, but I still get soft tabs. I tried (setq indent-tabs-mode t), but that doesn't seem to do anything.
chris
indent-tabs-mode is a buffer local variable. If you want to enable it globably use (set-default 'indent-tabs-mode t). This works for me for text mode at least. Some other modes may no take it into account. For instance ruby has `ruby-indent-tabs-mode` which will solve your problem for ruby. In css I have seen no effect so far, and can not find the variable responssable for that. Sorry. Apparently each mode's designer makes his own decision vis-a-vis the tabs functionality.
VitoshKa
I have played with all the xxx-indent-tabs-mode that I could think of, but they are still all soft.
chris
After coming back to emacs after having to work with textmate this week a lot of the tabs now work, except for css which give me 3 spaced soft tabs.
chris