2021-04-26 Binding a command to what is normally a prefix key

Some time ago, someone on the help-gnu-emacs asked how to rebind f2 so that it sets the default major mode for the current buffer. (The use case was that he wanted to change the mode of a buffer to perform some task, and then have an easy way to return to the “default” mode, based on the normal way Emacs uses to choose the major mode. That normal way is indeed a bit tricky.)

Well, there are in fact two questions hidden here. The former – resetting the major mode to “default” – is covered by normal-mode, which is a command doing exactly that. (The actual mode selection is done by the set-auto-mode function, invoked by normal-mode.) That one is fairly easy.

The latter question, however, is slightly more tricky. You cannot use the global-set-key to bind f2 to normal-mode interactively, since it is a prefix key by default. However, after unsetting it, it is possible. So, you can say M-: (global-unset-key [f2]) RET and then you are free to say M-x global-set-key RET f2 normal-mode RET. Of course, doing it in one step is also possible: M-: (global-set-key [f2] #'normal-mode) RET. Either way, I do not know of any way to do it without executing some Elisp code.

One more reason to learn a bit of Elisp if you are an Emacs user.

And by the way, I might have something to help learning a bit of Elisp in the very near future. Stay tuned!

CategoryEnglish, CategoryBlog, CategoryEmacs