sudeepdino008
sudeepdino008

Reputation: 3364

Changing key bindings in emacs

I want to change the commands for setting bookmarks, listing them and finding them in emacs. This is the emacs lisp code I have tried:

(global-set-key (kbd "C-c C-z") (kbd "C-x r m"))

This is failing. What is the correct method?

Upvotes: 1

Views: 772

Answers (3)

sds
sds

Reputation: 60004

If you want to define a key to do what some other key does, you want to do

(global-set-key (kbd "C-c C-z") (global-key-binding (kbd "C-x r m")))

If then you can rebind C-x r m to something else and C-c C-z will still be bound to bookmark-set (this is similar to file copying).

If, on the other hand, you want to make C-c C-z an alias for C-x r m, you need to use function-key-map:

(define-key function-key-map (kbd "C-c C-z") (kbd "C-x r m"))

in which case C-c C-z will be doing whatever C-x r m is doing even if you rebind the latter (this is similar to symbolic file links).

Upvotes: 4

abo-abo
abo-abo

Reputation: 20342

You should make use of Emacs' extensive help system:

  1. use f1 f with cursor on global-set-key to get information on how it should be called.
  2. use f1 k to find out which command any shortcut or menu item calls
  3. look the info page f1 i if you need more info. Use g (info) to learn how to use info.

Upvotes: 1

Adam Rosenfield
Adam Rosenfield

Reputation: 400204

The second argument of global-set-key needs to be the symbol of the function you want to run. In your case, the command C-x r m ordinarily corresponds to the bookmark-set (I determined this by running C-x k C-x r m), so you should pass 'bookmark-set:

(global-set-key (kbd "C-c C-z") 'bookmark-set)

Upvotes: 2

Related Questions