1

It is possible to jump to the function that a keybinding is mapped to (via describe-key). Is it possible to jump to where the keybinding itself is defined?

Example

(define-key my-mode-map "a" 'my-func) # line 1000 in a file.el

I want an interactive function jump-to-binding, which will read a binding ("a") and then open file.el and go to line 1000.

  • 1
    M-x find-variable RET my-mode-map RET will get you to where you want to be most of the time, but not always since the bindings may be added with a major-mode hook or wrapped in an eval-after-load statement. Another idea would be to grep for 'my-func or 'my-func) [using the appropriate backslashes if a regexp search] which may turn up a lot of hits, but then you can isearch for define-key in the grep results. – lawlist Jan 14 '18 at 23:34
  • Yeah find-variable combined with an appropriate grep seems to come pretty close to this. – Att Righ Jan 14 '18 at 23:39
  • Also this approach doesn't handle cases where you are doing dynamic / programmatic defining very well. It's interesting, the keybindings you make, and the builtins are very different in nature. Your keybindings will mostly get bound in one place, but never in the same place as the mode, the mode's keybindings get defined in lots of different places but as a rule away from the mode map. – Att Righ Jan 14 '18 at 23:53
1

For a key KEY, the following searchs (define-key _KEYMAP KEY CMD) expression in files where KEYMAP and CMD are defined.

;; -*- lexical-binding: t; -*-
(defun jump-to-binding (key)
  (interactive "kJump to binding of key: ")
  (let* ((cmd (key-binding key))
         (cmd-file (find-library-name (symbol-file cmd)))
         (keymap (help--binding-locus key nil))
         (keymap-file (find-library-name (symbol-file keymap)))
         (search-files (seq-uniq
                        (delq nil
                              (list cmd-file keymap-file)))))
    (require 'el-search-x)
    (el-search-setup-search
     (list '\` (list 'define-key ',_ (list '\, `(keys ,key)) `',cmd))
     (lambda () (stream search-files)))))

el-search is used for searching, you can get the package from GNU ELPA. The command can work for some keys such as C-s (isearch-forward) and C-l (recenter-top-bottom).

You can improve the command in many ways, for example,

  • Search bindings.el, many Emacs key bindings is created there
  • Search your init file if you want to
  • Support global-set-key, bind-key etc
  • Support prefix keymap, for example, C-x h should be able to find (define-key ctl-x-map "h" 'mark-whole-buffer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.