Emacs indent level global override

别说谁变了你拦得住时间么 提交于 2019-11-29 07:53:07

Indentation in Emacs isn't really a "trivial thing". You can read all about it at the Emacs Wiki:
http://www.emacswiki.org/emacs/CategoryIndentation

Any major mode is free to implement indentation however it wishes and, as you've noticed, several of them introduce indentation-related variables; so no, there is no global indentation configuration which is guaranteed to affect every possible major mode (although in practice, certain variables are completely standard by convention).

If not, is there a way to set them for each mode on startup?

Of course. The easiest way is to configure values and defaults using the M-x customize RET interface, although only variables defined with defcustom appear there, so it isn't necessarily comprehensive (but it can still be very useful for browsing some of the available settings, even if you don't actually use it to set the values).

Setting values (or defaults in the case of automatically buffer-local variables) in your init file with setq and setq-default, as you've done, is also fine.

If you want more control, you can use mode hooks. Pretty much every mode runs the list of functions assigned to the (mode-name)-hook variable after initialising itself in a buffer, so any mode-specific customisations can be written in an elisp function and added to the appropriate hook list, in your init file.

e.g.:

(defun my-c-mode-config ()
  (whitespace-mode 1)
  (setq indent-tabs-mode t
        tab-width        4
        c-basic-offset   4))

(add-hook 'c-mode-hook 'my-c-mode-config)

Use the variable standard-indent. You can set it in your startup file, or customize it; it's in the Indent group. Do M-x customize, then choose Editing, then Indent; alternatively, do M-x customize-group indent.

As for indenting with tabs instead of spaces, all you have to do is set indent-tabs-mode to t. It's customizable the same way.

(setq default-tab-width 2) works for me in emacs 24

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!