I find that there are two ways to set key in emacs: golbal-set-key and define-key. Are they the same? Or is there any pros/cons between the two approaches?
(global-set-key (kbd "C-c C-f") 'my-find-file)
(define-key global-map
"\C-ck" 'hello)
I find that there are two ways to set key in emacs: golbal-set-key and define-key. Are they the same? Or is there any pros/cons between the two approaches?
(global-set-key (kbd "C-c C-f") 'my-find-file)
(define-key global-map
"\C-ck" 'hello)
There's effectively no difference, if you look at the definition of global-set-key
you'll see:
(define-key (current-global-map) key command)
It's possible that (current-global-map)
will return a keymap that is different than global-key-map
, but unusual.
Now, since define-key
takes an argument of a keymap, it is obviously more flexible than the simple global-set-key
. For details on keymaps check out the info pages.
The difference is that (global-set-key)
or (local-set-key)
find out the global / local map for you (before calling (define-key)
).
Edit You can use M-x describe-function
for (global-set-key)
(global-set-key key command)
Give key a global binding as command.
command is the command definition to use; usually it is
a symbol naming an interactively-callable function.
key is a key sequence; noninteractively, it is a string or vector
of characters or event types, and non-ASCII characters with codes
above 127 (such as ISO Latin-1) can be included if you use a vector.
And for (define-key)
(define-key keymap key def)
In keymap, define key sequence key as def.
keymap is a keymap.
key is a string or a vector of symbols and characters meaning a
sequence of keystrokes and events. Non-ASCII characters with codes
above 127 (such as ISO Latin-1) can be included if you use a vector.
Using [t] for key creates a default definition, which applies to any
event type that has no other definition in this keymap.