3

By default Emacs uses tab instead of spaces for indentation, so I changed these 2 variables to change this behavior,

  (setq-default indent-tabs-mode nil)        ;; Disable indent with tabs
  (setq default-tab-width 4)                 ;; Set default indent width

It did change the indent mode from tab to space, but if I press RET, when I am in a programming buffer C++ for example, to enter a new line, I get 2 spaces of indentation instead of 4. How do I set this indentation level to 4 be default globally.

Also when when I am editing a line for example, it auto shift the line back to 2 space of indentation, for some reason, and also re-formats how I wrote the code, for example, I usually write like this,

if (true)
{
    // 4 spaces for indentation.
}

If I try to edit or add something, it pushes the lines back to 2 spaces of indentation and also sometimes re-formats the brackets position,

if (true) {
  // 2 spaces for indentation.
}

I am constantly fighting with this thing, and it gets annoying very quickly sometimes. As far as I know, I did not install any formatting package.

Drew
  • 75,699
  • 9
  • 109
  • 225
apoorv569
  • 113
  • 8
  • I'm guessing this question is a duplicate, but I don't have time to look for it now. Hopefully someone will find it, if so, and then close this as a dup. – Drew Jun 19 '21 at 13:38
  • The "sometimes re-formats the brackets position" issue is not a thing that Emacs does by default (to my knowledge). Although you say "As far as I know, I did not install any formatting package." I can only presume that you have unknowingly done exactly this. It's pretty easy to confirm -- just run `emacs -Q` and confirm the standard behaviour. Assuming it's your own config, you can use the recursive bisection technique to quickly narrow down which piece of your config is responsible. – phils Nov 16 '21 at 14:14
  • @phils I think the default `c-default-style` is set to `"gnu"` which indents the braces, as stated here - [EmacsWiki](https://www.emacswiki.org/emacs/IndentingC#h5o-2) I have now switched to `linux` style, but I don't see any difference. At this point I am mostly manually editing brace position every time. I like how `doom-emacs` has the perfect `c-style` that I prefer, wish they had a package to set it up. – apoorv569 Nov 17 '21 at 22:53
  • Indentation can certainly happen automatically, but the "move a bracket from the start of the current line to end of the previous line" issue is not something that I've ever experienced with Emacs, so regardless of the chosen style I don't *think* that particular kind of reformatting is standard behaviour. – phils Nov 17 '21 at 23:40
  • If you are still having problems with the indentation levels, though, that's definitely fixable. – phils Nov 17 '21 at 23:41
  • @phils I think you are correct, some other package or settings is changing the brace position automatically when I hit `RET`. BTW I found the `doom-emacs` `c-style` [here](https://github.com/hlissner/doom-emacs/blob/df64e5024b036ffeadf26226a7fa28473d270ba0/modules/lang/cc/config.el#L81) is the link. Now the question is how do I see what package is causing this. I don't have any package that formats the code. I only have `LSP` and `ccls` for `c/c++`. I do have a lot of other packages as well. [Here](https://gitlab.com/apoorv569/dotfiles/-/blob/master/.emacs.d/Emacs.org) is my `emacs` config. – apoorv569 Nov 18 '21 at 05:51
  • Looking at the `*Message*` buffer I saw these messages when the reformatting happens, `LSP :: Applying 3 edits to 'MainFrame.cpp' ... Applying 3 edits to 'MainFrame.cpp' ...done` I think its `LSP` that is re formatting the code. – apoorv569 Nov 18 '21 at 06:04
  • Should be easy to test that by disabling LSP. Looks like a good lead; hopefully you can figure out from here how to make it behave how you want. – phils Nov 18 '21 at 06:09
  • Ok so I found this issue on [emacs-lsp github](https://github.com/emacs-lsp/lsp-mode/issues/1342), looks like I have to set `lsp-enable-on-type-formatting` to `nil` and it worked. It no longer formats the code. The `doom c-style` is kind of working, but not exactly as it does in doom. When I type for example `void func()` and press `RET` to go to next line and create `{}` and my cursor is between the 2 braces i.e `{|}` and I press `RET` in `doom` it automatically create new line, indents 4 spaces and take the closing brace to new line. I want to achieve that same behavior. – apoorv569 Nov 18 '21 at 06:45

1 Answers1

2

There is no global setting for the indentation width. Instead, each mode has it’s own setting. In c-mode it is called c-basic-offset. In js2-mode it is js2-basic-offset. For lisp-mode there are two: lisp-body-indent and lisp-indent-offset.

You’re probably using c-mode for editing your C++ files, so you should customize the c-basic-offset variable. Run M-x customize-variable then enter the variable name that you would like to change.

db48x
  • 15,741
  • 1
  • 19
  • 23
  • I'm using `c++-mode` to edit `C++` files. And the value for `c-basic-offset` is set to `set-from-style`. So I just change this to 2 or 4, whatever level of spaces I need? There is no `c++-basic-offset` it seems like. – apoorv569 Jun 19 '21 at 09:59
  • Oh, right. For `c-mode` (and therefore for a number of modes derived from it, such as java, awk, csharp, and c++ modes) there is also a more complicated style system that lets you pick where your curly braces go, and other things. You can change the current style by customizing `c-default-style`; it takes values that are defined in `c-style-alist`. Use `C-h v` to look at the help for all of these variables to find out more. – db48x Jun 19 '21 at 10:14
  • I think the indentation problem is fixed now I add `custom-set-variables '(c-basic-offset 4))` to my `init.el` file, and it seems to have fixed it. But the formatting problem is still there. – apoorv569 Jun 19 '21 at 16:07