2

In light that this may be a duplicate, but I did not find the answer to my question scouring this site.

The default behavior of GNU Emacs appears to have electric-indent-mode enabled, which is fine almost everywhere. However, I would like to change the behavior for trimming whitespace at the end of the line and auto-indenting after pressing Return for only one mode: Fundamental.

In essence, I would like Enter to simply add a newline (electric-indent-just-newline) while pressing Alt+Enter to insert the default newline.

How would I go about it? I found this, but I do not seem to get it converted to work with Fundamental.

1

This is a partial answer to your question, as I could not yet figure out, how to run a hook on fundamental-mode only, but please read on.

With the following command, you can activate the Enter behaviour you like. Just run this command with M-x from the current buffer. (It enables this keybinding in all buffers with the same major-mode)

(defun my-fundamental-newline ()
  (interactive)
  (local-set-key (kbd "<RET>") #'electric-indent-just-newline)
  (local-set-key (kbd "M-<RET>") #'newline))

I got the idea from this question/answer


Then please read this question/answer to get info, why it is difficult to hook this function to fundamental-mode.

  • Thank you! This is an interesting approach. Initially I used global keys. It was great until I realized what impact it had on other modes. Your second link gave me the answer then. Not what I was hoping for, but it makes it clear that Fundamental mode is not to be played around with. In essence I understand that for something like that I would have to write my own mode duplicating Fundamental, plus the modifications I want to have. – Phoenix Nov 19 at 11:49
1

electric-indent-mode is turnedd on by default since Emacs 24.4, according to C-u C-h n 24.4.


Fundamental mode is not an ordinary major mode, it's the default major mode for temporary buffers and Emacs creates lots of temporary buffers all the time, such as when you use minibuffer/echo area and make network requests. For example, right now I have 20 buffers in Fundamental mode (note that they are created automatically, such as, *Echo Area 1*):

(seq-count
 (lambda (b) (with-current-buffer b (eq 'fundamental-mode major-mode)))
 (buffer-list))
;; => 20

It might be a good reason that Fundamental mode doesn't have a mode keymap and hook. And users should use more specialized major modes such as Text mode instead of Fundamental mode. If you are not satisfied with Fundamental mode, you should use another major mode instead of trying to extend it.


Regarding your question, if I want only a newline, I use the default C-j (electric-newline-and-maybe-indent). And since you like different key bindings, you should customize them using a minor mode or major mode keymap.

  • Thank you for another insight! For some reason I was oblivious to Text mode. Though my initial attempts with it were in vain, once I have a bit more time on my hands (e.g. tonight or the weekend), I will experiment around with it a bit more. – Phoenix Nov 19 at 12:32

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.