Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given an emacs command name, how would you find key-bindings ? (and vice versa)

Tags:

emacs

If I know an emacs command name, says, "goto-line"; what if I want to query whether if there are any key-sequences bound to this command ?

And vice versa, given a key sequence, how can I find its command name ?

like image 869
Sake Avatar asked Jun 08 '09 14:06

Sake


People also ask

What is key bindings in emacs?

Keys can be bound to commands either interactively or in your . emacs file. To interactively bind keys for all modes, type M-x global-set-key RET key cmd RET . To bind a key just in the current major mode, type M-x local-set-key RET key cmd RET .

What is the Return key in emacs?

"RET" is the Return key while emacs runs in a terminal. and run emacs in terminal, your keybinding will have no effect.


2 Answers

To just find key bindings for a command, you can use emacs help's "where-is" feature

C-h w command-name 

If multiple bindings are set for the command they will all be listed.

For the inverse, given a key sequence, you can type

C-h k key-sequence 

To get the command that would run.

You can get detailed information about a command, also any non-interactive function defined, by typing

C-h f function-name 

Which will give you detailed information about a function, including any key bindings for it, and

C-h v variable-name 

will give you information about any (bound) variable. Key-maps are kept in variables, however the key codes are stored in a raw format. Try C-h v isearch-mode-map for an example.

For more help on getting help, you can type

C-h ? 
like image 190
Adam Rosenfield Avatar answered Sep 28 '22 16:09

Adam Rosenfield


For interactively getting the command bound to a keyboard shortcut (or a key sequence in Emacs terms), see the selected answer.

For programmatically getting the command bound to a given key sequence, use the function key-binding or lookup-key that takes a key sequence and returns its bound command. The function key-binding is what C-h k uses.

(key-binding (kbd "C-h m")) 

returns the command bound to C-h m by searching in all current keymaps. The function lookup-key searches in a single keymap:

(lookup-key (current-global-map) (kbd "TAB")) ; => indent-for-tab-command (lookup-key org-mode-map (kbd "TAB")) ; => org-cycle (lookup-key text-mode-map (kbd "TAB")) ; => nil (lookup-key isearch-mode-map (kbd "TAB")) ; => isearch-printing-char 

For programmatically getting all key sequences bound to a given command, where-is-internal is probably the function to use. The name of the function ending with internal seems to suggest that it's not for Emacs users to use in their init files but this function having a docstring seems to suggest otherwise. Anyone considering use of where-is-internal should first check if remapping keys instead can achieve their goal.

An alternative for finding the keys that are bound to a specific command (e.g., forward-char) is substitute-command-keys (e.g., (substitute-command-keys "\\[forward-char]")). That is especially useful in larger texts.

like image 34
Jisang Yoo Avatar answered Sep 28 '22 16:09

Jisang Yoo