9

https://stackoverflow.com/questions/27077770/how-do-i-disable-electric-indent-on-ret-but-still-keep-other-electric-characters asks and answers how to disable electric indent when pressing RET. All similar questions also seem to stop at "disable electric-indent-mode".

Out-of-the-box, RET indents the previous line and the new line. For the new line this means that it simply adds enough whitespace to make the cursor appear at the correct position.

I like the second part of the behaviour but not the first, i.e. I never want it to indent the previous line. Is there a way to change it so it gives me enough whitespace on the new line but keeping the previous line untouched?

I actually don't need/want most of the electricity. A solution which involves turning off electric-indent-mode but somehow activating the "add whitespace for new lines" feature would be fine with me.

Thanks!

6

I think you'll get what you want with

(setq-default electric-indent-inhibit t)

This was meant as a variable for major-modes (rather than for users), but it should take care of your use-case as well.

  • Awesome, thank you. I stumbled upon this variable while sifting through electric.el but put it aside due to its documentation: "If non-nil, reindentation is not appropriate for this buffer." My interpretation of that was that it would simply disable electric-mode for this buffer... – AnoE Mar 10 '16 at 17:33
  • The key is that it talks about reindentation, i.e. changing the indentation of an existing line, as opposed to indentation of a new line. – Stefan Mar 10 '16 at 23:19
  • I like electric indent mode when I type a curly brace (for the current line). Any way to keep this behavior while preventing the previous line from changing when I press enter? – Stanley Bak Jun 27 at 15:19
1

I actually don't need/want most of the electricity. A solution which involves turning off electric-indent-mode but somehow activating the "add whitespace for new lines" feature would be fine with me.

This approach might be of interest:

In programming modes I invariably want RET to invoke indent-new-comment-line or its mode-specific analogue, which always has a default binding of M-j. These functions take care of indentation, and also comment continuation when you're inside one.

(defun my-coding-config ()
  ;; ...
  (local-set-key (kbd "RET") (key-binding (kbd "M-j")))
  (local-set-key (kbd "<S-return>") 'newline)
  )

(mapc
 (lambda (language-mode-hook)
   (add-hook language-mode-hook 'my-coding-config))
 '(prog-mode-hook
   ;; plus anything not derived from prog-mode:
   inferior-emacs-lisp-mode-hook
   css-mode-hook
   python-mode-hook))

(add-hook 'js-mode-hook 'my-js-mode-hook)
(defun my-js-mode-hook ()
  ;; Fix M-j behaviour in block comments in js-mode
  (setq-local comment-multi-line t)
  (local-set-key [remap indent-new-comment-line] 'c-indent-new-comment-line))

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.