38

If you had two snippets:

(global-set-key "\C-d" delete-char)

and

(define-key global-map "\C-d" delete-char)

Is there a difference between the two? If so, when would you use one over the other?

haxney
  • 3,358
  • 4
  • 30
  • 31

2 Answers2

35

Function global-set-key is an interactive function based on define-key which you can invoke by typing M-x global-set-key. Function define-key is rather used in Lisp programs.

You can look up global-set-key's source code with C-h f global-set-key to see that it only wraps define-key.

To answer your question, there are no significant differences between them.

Community
  • 1
  • 1
viam0Zah
  • 25,949
  • 8
  • 77
  • 100
22

global-set-key is defined in subr.el as:

(define-key (current-global-map) key command))
dfa
  • 114,442
  • 31
  • 189
  • 228
  • 4
    It will be nice if you can tell the difference b/w the 2 – Jaseem May 01 '13 at 20:35
  • @Jaseem He just did. That's the difference between the two. `global-set-key` is a shortcut for defining a key in the value of `(current-global-map)`. –  Dec 12 '16 at 00:53