jp_15
jp_15

Reputation: 11

How can I change the behavior of a key in Emacs?

I want to change the behavior of the key TAB. I don't know if it's possible: I want to force TAB to do what M-i does.

Upvotes: 1

Views: 320

Answers (1)

Drew
Drew

Reputation: 30718

Sounds like you want to remap the command that is bound to M-i to TAB.

If you want to do that in the global-map then this does it:

(global-set-key (kbd "TAB") (kbd "M-i"))

If you want to do it in a particular keymap, say foo-map, then this does it:

(define-key foo-map (kbd "TAB") (kbd "M-i"))

But typically users want to remap a command's keys to another command. For that you use [remap OLD-COMMAND] as the first arg to global-set-key or the second arg to define-key, and you use the NEW-COMMAND as the last arg.

Alternatively, for that you can use function substitute-key-definition, which also lets you change keymaps. C-h f substitute-key-definition starts with this:

substitute-key-definition is a compiled Lisp function in subr.el.

(substitute-key-definition OLDDEF NEWDEF KEYMAP &optional OLDMAP)

Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.

In other words, OLDDEF is replaced with NEWDEF wherever it appears. Alternatively, if optional fourth argument OLDMAP is specified, we redefine in KEYMAP as NEWDEF those keys which are defined as OLDDEF in OLDMAP.

Upvotes: 2

Related Questions