在emacs中设置每个项目的缩进变量

aza*_*gru 1 emacs indentation locals

我的emacs设置为全局使用2个空格进行缩进:

(setq-default indent-tabs-mode nil)
(setq tab-width 2)
(setq js-indent-level 2)
(setq css-indent-offset 2)
Run Code Online (Sandbox Code Playgroud)

但我想贡献一个web项目(主要是html,css和js),它使用4个空格进行缩进.所以我想.dir-locals.el在项目目录中设置一个文件.该文件具有以下设置(随add-dir-local-variable命令添加):

((nil . ((tab-width . 4)
         (js-indent-level . 4)))

;;; Directory Local Variables
;;; See Info node `(emacs) Directory Variables' for more information.

((js-mode
  (tab-width . 4)))
;;; Directory Local Variables
;;; See Info node `(emacs) Directory Variables' for more information.

((js-mode
  (js-indent-level . 4)))
;;; Directory Local Variables
;;; See Info node `(emacs) Directory Variables' for more information.

((html-mode
  (tab-width . 4)))
Run Code Online (Sandbox Code Playgroud)

但这些设置不会生效.当我在项目子目录中打开.js或.html文件时,按Tab键会缩进2个空格.

我究竟做错了什么?

phi*_*ils 5

对于初学者,您的.dir-locals.el数据是不平衡的(M-x check-parens).

我不确定会发生什么事,但如果你能让Emacs这样做,那么你应该这样做M-x report-emacs-bug.我假设它来自手动编辑.

我不确定这些多项 是否js-mode有效.也许那很好,但似乎不寻常.(可能Emacs对不平衡的parens感到困惑.)

这是您的文件重写,以使用更常见(或至少记录)的点对符号:

((nil . ((tab-width . 4)
         (js-indent-level . 4)))

 (js-mode . ((tab-width . 4)
             (js-indent-level . 4)))

 (html-mode . ((tab-width . 4))))
Run Code Online (Sandbox Code Playgroud)

请注意(对于此特定数据)您不需要js-modehtml-mode条目,因为它们复制了nil模式条目的默认值.

编辑:实验上,add-dir-local-variable一旦文件处于有效状态,似乎表现如预期.

它更喜欢在可能的地方创建更紧凑的列表符号 - 这很好; 它们是等价的 - 但要注意格式差异是有用的.
看到C-hig (elisp) Dotted Pair Notation RET