11

Looking at how people program their keyboard shortcuts in Emacs, I have noticed two different patterns:

One uses:

(global-set-key (kbd "M-N) 'select-next-window)

The other one:

(define-key global-map (kbd "M-N") 'select-next-window)

What's the difference between them? Don't they both define "global" keyboard shortcuts in Emacs?

1 Answers1

6

Doesn't seem to be much of a difference in the major case:

  • (global-set-key key command) boils down to (define-key (current-global-map) key command)), and
  • describe-var tells us that global-map "is a keymap which is usually (but not necessarily) Emacs's global map", whereas
  • (current-global-map) "returns the current global keymap".

However, the Emacs docs talk of "the" global keymap, so it's unclear how, if ever, you can come across several global keymaps.

  • The variable global-map might be bound to a file or buffer local value, or it could even be bound in a let statement. – Trey Jackson Dec 15 '11 at 23:09
  • 2
    The global keymap is set by use-global-map. There aren't many packages that do this: mostly a few emulation modes such as Viper, and some functions such as read-char that override the global map temporarily. – Gilles 'SO- stop being evil' Dec 16 '11 at 08:11