reddit / emacs

Random `c-basic-offset` (set to 5!)

Zeta611 · 2022-09-15 01:21
Hi, I've been trying *really* hard to get C indentation correct, that is, the K&R style. I thought I got it right, but I found out that the default value of `c-basic-offset` was 5 on my Mac! It was fine on my Linux machine (running Manjaro) though. So I had to manually set it to 8, and now I'm quite happy with my setup. The following is the relevant config: ```lisp (setq-default indent-tabs-mode nil) (setq-default tab-width 8) (setq backward-delete-char-untabify-method 'hungry) (setq-default c-default-style "k&r") (setq-default c-basic-offset 8) (add-hook 'c-mode-common-hook (lambda () (setq indent-tabs-mode t))) (add-hook 'c-mode-common-hook 'lsp) ;;(use-package smart-tabs-mode ;; :ensure t ;; :config ;; (smart-tabs-insinuate 'c)) (setq whitespace-style '(face tabs tab-mark trailing)) (custom-set-faces '(whitespace-tab ((t (:foreground "#636363"))))) (setq whitespace-display-mappings '((tab-mark 9 [124 9] [92 9]))) ; 124 is the ascii ID for '\|' (global-whitespace-mode) ; Enable whitespace mode everywhere ``` I had to comment out `smart-tabs-mode` due to some [weird error](https://github.com/jcsalomon/smarttabs/issues/53) on my Mac, tho. But I'm still curious about that `c-basic-offset`. Any idea where this random value of "5" came from on my Mac? Can it be different in each distribution of Emacs? (FYI, I use the same configuration file for both my Mac and my Linux machine.)
redguardtoo · 2022-09-15 02:12
From emacs code,, ```lisp (defcustom-c-stylevar c-basic-offset 4 "Amount of basic offset used by + and - symbols in `c-offsets-alist'. Also used as the indentation step when `c-syntactic-indentation' is nil." :type 'integer :group 'c) ;;;###autoload(put 'c-basic-offset 'safe-local-variable 'integerp) ``` So it comes from your setup or 3rd party plugin you use. You can try setting up `c-basic-offset` in major mode hook to fix it.