0

I'm a bit confused about setting indentation for different modes in Emacs.

  • It seems you have to explicitly set indentation for each mode
  • Some modes use setq-default, some use setq and some use add-hook
  • Indentation variables are inconsistently named

My question:

Is there a single indentation variable I can set that all modes inherit as a default?

If not, is there a shorter and more correct way to set up my indentation than listed below?

(setq-default tab-width 2)
(setq-default sh-basic-offset 2)
(setq-default sh-indentation 2) ;; workaround for bug in 25.3.1
(setq c-basic-offset 2
      css-indent-offset 2
      js-indent-level 2)
(add-hook 'php-mode-hook '(lambda ()
                            (setq c-basic-offset 2)))
(add-hook 'ess-mode-hook '(lambda ()
                            (setq ess-indent-offset 2)))
  • I think you could use (setq-default c-basic-offset 2) and then not need to set it in the hook. The general answer is "no", as wasamasa has already written. – npostavs Mar 17 '18 at 10:58
  • 1
    I suspect that's php-mode overriding c-basic-offset per buffer. – wasamasa Mar 17 '18 at 11:05
  • Maybe it would be possible to attempt this sort of thing by adding a prog-mode hook that tries multiple variable names and sets whichever one looks like the indentation setting, but that's a hacky way to go about it. – DoMiNeLa10 Mar 17 '18 at 13:02
2

No, there isn't. Unlike other text editors and IDEs where all language support comes from one hand Emacs is a hodge-podge of modes of wildly varying quality. Every mode may define its own way of customizing indentation (note the may, some modes deliberately ignore that topic), so unless you go the DIY route, it's perfectly normal to end up with that kind of init file.

If you forego the smart indentation feature, you could define your own indentation command that just inserts an indentation block. That would be kind of silly (there are programming languages where indentation is more complicated, such as the Lisp and ML family), but give you the consistency you seek for.

  • 1
    "you could define your own indentation command that just inserts an indentation block. That would be kind of silly" - also, your Lisp code would be unreadable. – npostavs Mar 17 '18 at 10:56
  • Good point, I keep seeing that kind of thing over and over again. – wasamasa Mar 17 '18 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.