reddit / emacs

How to Change Autoindent from 2 to 4

DOMINATORLORD9872 · 2022-09-04 02:36
Hello, new Emacs user. I've been trying to run through the docs and test out writing some simple hello world when I noticed something when indenting. When I start writing code: ```c ... int main() { if (a == b) ... } ``` instead of: ```c ... int main() { if (a == b) } ... ``` I get this: ```c int main() { if (a == b) ... } ``` Even after inserting a literal TAB character it just goes back to 2. Is there a way to change this behavior so that its auto indenting is 4, possibly globally?
lebensterben · 2022-09-04 03:10
(setq tab-width 2)
flexibeast · 2022-09-04 03:11
The relevant setting in the general case is the `tab-width` variable, but individual modes can have their own settings. Which mode are you in? c-mode?
DOMINATORLORD9872 · 2022-09-04 03:29
Yeah, I was in c-mode
mauguro_ · 2022-09-04 03:30
how do you config that for a specific mode?
flexibeast · 2022-09-04 03:33
It depends on the mode (assuming the mode has such a setting in the first place). If the mode has been written correctly, you can do `M-x customize-group [mode]` (e.g. `M-x customize-group c` for c-mode) to check out what options are available.
flexibeast · 2022-09-04 03:38
Okay, so the value of 2 probably comes from the active`c-style` being `gnu` (cf. the contents of the `c-style-alist` variable). i'm no familiar with c-mode myself, but check out the documentation for the `c-set-style` function (`C-h f c-set-style`). Hopefully others will have more specific suggestions.
VernonGrant · 2022-09-04 05:35
Please take a look at [Editor Config](https://editorconfig.org). It makes handling indentation specifics easy, here a link to the Emacs [package](https://github.com/editorconfig/editorconfig-emacs). I also set these defaults in my Emacs init file: (setq-default indent-tabs-mode nil) (setq-default c-basic-indent 4) (setq-default tab-width 4)
DOMINATORLORD9872 · 2022-09-04 10:29
I'll look into that!
DOMINATORLORD9872 · 2022-09-04 15:42
okay I just looked into `c-set-style` and searched up `c-basic-offset` and set it to 4 and I was able to get the auto-indentation to work properly! I added this to my `.emacs` file (A similar solution is most likely available for your mode): (setq-default c-basic-offset 4) Edit: Just did a bit of a test run with a Python file and it said that it wasn't able to find out the indentation style so it defaulted to 4. That is most likely something to do with my configuration. Indent config below: (setq-default indent-tabs-mode nil) (setq-default c-basic-indent 4) (setq-default c-basic-offset 4) (setq-default tab-width 4) Edit: If you want to change the offset for Lisp (and possibly other Lisp dialects, e.g. Clojure & Common Lisp - I used Common Lisp (.lsp) for this test), put this in your config file: ``` (setq-default lis-indent-offset 4) ```