8

I've just tried

(set 'global-map (make-sparse-keymap))

but issuing C-hb still shows tons of crap, especially in the Global Bindings section:

Global Bindings:
key             binding
---             -------

C-@             set-mark-command
C-a             move-beginning-of-line
C-b             backward-char
C-d             delete-char
C-e             move-end-of-line
C-f             forward-char
C-g             keyboard-quit
C-h             help-command
TAB             indent-for-tab-command
C-k             kill-line
C-l             recenter-top-bottom
RET             newline
C-n             next-line
C-o             open-line
...             ...

and there are so many more to come.

I would like to get rid of these completely. How can I possibly do that in one simple function call? I would like the solution to be a total wipeout, rather than a loop which is setting all of these maps to nil. Thank you in advance.

NOTE: Please, no teaching answers or spam comments like "These keys are essential for productivity, don't do that!". I have highly customized emulation layer for modal text editing here, so I don't need these default bindings at all.

  • I'm curious. What's wrong with evil-mode that you have to write your own modal editing layer? – Tu Do Nov 25 '14 at 11:13
  • @Tu Do, it is evil-mode, but some of its top level functionality: operators, text-objects, and some commands were rewritten or extended somehow. Its default bindings were also totally wiped out and rearranged across the keyboard from the very beginning. This however has nothing to do with the question. – Alexander Shukaev Nov 25 '14 at 11:22
  • I was just asking for curiosity. But thanks. – Tu Do Nov 25 '14 at 11:59
  • You should probably use a non-sparse keymap created with make-keymap. – tarsius Nov 26 '14 at 3:19
17

Taken from the Emacs Lisp Manual (see Controlling Active Maps):

(use-global-map (make-sparse-keymap))

To quote the manual: "It is very unusual to change the global keymap.". Safety note: Make sure you can save buffers and cleanly exit Emacs afterwards with a different keymap, otherwise you'll have to kill it without saved changes.

6

Although the following information is not a direct answer to the question (since it was already answered by @wasamasa), I still believe that it is an essential addition to the answer. The point is that after issuing

(use-global-map (make-sparse-keymap))

one indeed wipes out the Global Bindings section completely, and as a consequence, one cannot even type text anymore! In overwhelming majority of cases, one of course would want to restore the ability to type text as it is essential for any text editor anyway. Thus, here is the one way to do that:

(global-set-key [t] #'self-insert-command)
(let ((c ?\s))
  (while (< c ?\d)
    (global-set-key (vector c) #'self-insert-command)
    (setq c (1+ c)))
  (when (eq system-type 'ms-dos)
    (setq c 128)
    (while (< c 160)
      (global-set-key (vector c) #'self-insert-command)
      (setq c (1+ c))))
  (setq c 160)
  (while (< c 256)
    (global-set-key (vector c) #'self-insert-command)
    (setq c (1+ c))))

Just so that you guys believe that this Emacs Lisp code is a correct way to do that, I will provide the source which I used to produce it. By default the ability to type text with the current 8-bit ASCII code page is implemented in the cmds.c translation unit of the Emacs C source code:

  for (n = 040; n < 0177; n++)
    initial_define_key (global_map, n, "self-insert-command");
#ifdef MSDOS
  for (n = 0200; n < 0240; n++)
    initial_define_key (global_map, n, "self-insert-command");
#endif
  for (n = 0240; n < 0400; n++)
    initial_define_key (global_map, n, "self-insert-command");

As you can see, the Emacs Lisp code above is the exact replication of this C code, except for one additional line:

(global-set-key [t] #'self-insert-command)

Consider it a caveat, but this line is important to have! Otherwise only 7-bits of the current ASCII code page would be available (regardless of the fact that we have initialized all the 8-bits).

  • 3
    This seems unduly complex. Why not copy the self-insert-command bindings from the current global keymap? (setq my-global-map (make-keymap)) (substitute-key-definition 'self-insert-command 'self-insert-command my-global-map global-map) – Gilles Nov 25 '14 at 22:28
  • 1
    @Gilles, I don't see what you find complex in 3 loops, but feel free to go your way if you have already tested it. Also, this post is more of an educational value, so that people who wipe out global-map immediately know what problem they are going to face and what is the recipe to solve it. They will probably learn something new about ASCII in general and/or how it is implemented in Emacs in particular. – Alexander Shukaev Nov 25 '14 at 23:19

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.