(global-set-key [?\S- ] 'set-mark-command) 

(define-key global-map (kbd "C-c SPC") 'ace-jump-mode)

What's the differences and when to use what?


The manual node on Commands for Key Bindings, which I got from the first hit of a google search for "emacs global-set-key", says they're the same:

Command: global-set-key key binding

This function sets the binding of key in the current global map to binding.

         (global-set-key key binding)
         (define-key (current-global-map) key binding)

If you look at the function definition for global-set-key (C-h f global-set-key, then follow the link to the source code), the function only does the following (ignoring the error checking code):

(define-key (current-global-map) key command)

And the docstring for current-global-map (again, C-h f current-global-map) says:

Return the current global keymap.

| improve this answer | |
  • The docs aren't always right, @Dan :) – abo-abo Dec 28 '14 at 13:20
  • @abo-abo: +1 for the interesting edge case in your answer. My answer was meant more for pedagogical purposes: how one would go about answering this question on one's own. – Dan Dec 28 '14 at 13:53

It's more or less the same. The first thing expands to:

(progn (or (vectorp [?\S-]) (stringp [?\S-])
           (signal 'wrong-type-argument (list 'arrayp [?\S-])))
       (define-key (current-global-map) [?\S-] 'set-mark-command))

And since:

(eq (current-global-map) global-map)
; -> t

it's mostly equivalent to the second thing. I do prefer the global-set-key though, since they're nicer semantic-wise.

update: they aren't the same

(setq foobar (copy-keymap global-map))
(define-key foobar (kbd "C-.") 'forward-char)
(use-global-map foobar)
(eq (current-global-map) foobar)
;; -> t
(eq (current-global-map) global-map)
;; -> nil 
| improve this answer | |

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.