开发者

How can I set Emacs tab settings by file type?

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.


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.


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.


js-mode uses js-indent-level so put (setq js-indent-level 4) into your ~/.emacs (shouldn't have to be in a hook, even, but if you're wondering, it's js-mode-hook, not javascript-mode-hook).

If setting tab-width doesn't change your indentation level for a certain mode, it's often simplest to just open the source for that mode. I found this variable by doing C-h f js-mode, clicking the link "js.el", then searching for "indent", second hit from the top.


However, if you collaborate a lot with other people, it's often better to put a cookie at the top of the file. I typically do // -*- tab-width: 8 -*- in the file, and then I have stuff like this in my ~/.emacs:

(defvaralias 'c-basic-offset 'tab-width)
(defvaralias 'cperl-indent-level 'tab-width)
(defvaralias 'perl-indent-level 'tab-width)
(defvaralias 'js-indent-level 'tab-width)

so that I have less variables to deal with (and don't have to get warnings about the file-local variable being unsafe or whatever if the mode-writer forgot to declare it as safe)


If you are using ELPA's css-mode.el with emacs 23.1.1, you can parametrize the global setting for tab width for CSS files for the tab width by doing the following: 1) Type M-x customize-variable , 2) Then type css-indent-level, 3) Then after you change the variable to your liking, you do "Save for future sessions".


For HTML and erb: if you are using web-mode (the mode provided by Spacemacs) it can be as simple as:

(setq-default web-mode-code-indent-offset 2 web-mode-markup-indent-offset 2)

where markup-indent-offset refers to the actual tags and code-indent-offset refers to embedded Ruby in ERB, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜