I need to be able to set the tab settings for the following file types:
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.
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]
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.
This is because standard tabs are set to eight spaces. Tabs are special characters.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With