6

Before Emacs 24.4, indentation in c-mode happened after typing things like ;, , and so on and I like this behavior.

But in Emacs 24.4, c-mode also indents on RET. This causes a lot of problems for me because if I want to insert a blank line, I need to press RET and then delete any spaces or tabs that electric- inserts.

So I would like to disable indent on RET, but keep all other electric-indent functionality, like indent on ;.

How can I achieve this?

I tried calling (electric-indent-local-mode -1) in c-mode hooks, but this disabled not only indent on RET, but also indent on ;, , and so on.

2
  • FWIW, the way I do it is that I hit RET either at the beginning of the next line, or at the of the previous line, depending on whether I just want to insert a blank line, or intend to actualy type text after thi RET.
    – Stefan
    Commented Nov 7, 2014 at 14:22
  • FWIW, the news for release 24.4 notes that electric-indent-mode is now enabled by default; RET now inserts a newline and indents, while C-j inserts a newline but does not indent (basically swapping the functionality of RET and C-j from previous versions).
    – Dan
    Commented Nov 7, 2014 at 14:36

3 Answers 3

4

You could rebind RET to electric-indent-just-newline like this:

(add-hook 'c-mode-hook
          (lambda() (local-set-key (kbd "<RET>") 'electric-indent-just-newline)))
2
  • Please don't quote your lambdas.
    – Stefan
    Commented Nov 7, 2014 at 16:13
  • @Stefan: Thanks! I removed the redundant quote.
    – paprika
    Commented Nov 7, 2014 at 17:48
4

You want to do:

 (add-hook 'c-mode-hook
           (lambda ()
             (setq-local electric-indent-chars
                         (remq ?\n electric-indent-chars))))
1
  • Thanks, this looks more right solution, then rebinding RET.
    – fghj
    Commented Nov 7, 2014 at 15:53
0

Can't you simply use C-j to insert empty lines on Emacs 24.4?

5
  • 1
    No, pressing enter is conditioned reflex, why I should change my head firmware, instead of computer firmware
    – fghj
    Commented Nov 7, 2014 at 10:52
  • Fortunately you're not a computer, I believe. :) Commented Nov 7, 2014 at 11:31
  • 1
    @mdallastella: this response would probably be better as a comment on the question rather than an answer to it.
    – Dan
    Commented Nov 7, 2014 at 14:41
  • Drat: and by "response," I mean your original answer, not your commented response to OP's comment.
    – Dan
    Commented Nov 7, 2014 at 14:49
  • You're right, but I thought the op didn't know about C-j. Commented Nov 8, 2014 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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