Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set Emacs tab settings by file type?

Tags:

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.

like image 902
chris Avatar asked Oct 23 '10 20:10

chris


People also ask

How do I set tabs in Emacs?

Make Tab Key Do Indent or CompletionTo set globally, use setq-default (but major modes may override it.) To set for current buffer only, use setq. To set for specific major mode only, use setq in a hook. [see Emacs: What is Hook]

How do you set a tab stop in Emacs?

The convenient way to set the tab stops is with M-x edit-tab-stops , which creates and selects a buffer containing a description of the tab stop settings. You can edit this buffer to specify different tab stops, and then type C-c C-c to make those new tab stops take effect.

How many spaces is a tab in Emacs?

This is because standard tabs are set to eight spaces. Tabs are special characters.


1 Answers

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.

like image 130
VitoshKa Avatar answered Oct 27 '22 11:10

VitoshKa