1

I want to bind f5 to launch pdb, but the minibuffer always says <f5> is undefined.

(use-package python-mode
  :bind (:map python-mode-map
          ("<[f5]>" . realgud:pdb))
  :config
  (load-library 'realgud))
8
  • Maybe try (kbd "<f5>") ?
    – NickD
    Commented Jul 26, 2021 at 1:07
  • @NickD It wants arguments acceptable to the bind-keys function, so that doesn't work.
    – Brady Dean
    Commented Jul 26, 2021 at 1:12
  • Does the vector form [f5] work? In case, it's not clear, I know nothing about use-package, so you might want to ignore me and wait for somebody who knows something about it.
    – NickD
    Commented Jul 26, 2021 at 1:13
  • No, that gives a parse error. I got this syntax from emacs.stackexchange.com/questions/3886/…
    – Brady Dean
    Commented Jul 26, 2021 at 1:14
  • Does that (:map...) sexp need to be quoted? Or more likely, does the cons ("<[f5]>" . realgud:pdb) need to be quoted? The question you cited shows no quotation, but it also doesn't show that you can use a :map sexp. [IMHO it's too bad that things like use-package (apparently) introduce another syntax for key binding. The existing syntax is already confusing enough.)]
    – Drew
    Commented Jul 26, 2021 at 2:01

2 Answers 2

0

Try this:

(use-package python-mode
  :bind (:map python-mode-map
          ([f5] . realgud:pdb)))

I can't see any reason why this would not work, but I should note that it's derived from my specific usage, which is slightly different.

I collect all my F-key bindings in an alist, directly calling bind-key on the car and cdr of each element. bind-key is what a :bind stanza in a use-package declaration expands to.

(defun ph/map-keys (key-mappings)
  "Bind keys using alist KEY-MAPPINGS."
  (dolist (key-mapping key-mappings)
    (let ((key-chord (car key-mapping)) (command (cdr key-mapping)))
      (when (and key-chord command (commandp command))
        (bind-key key-chord command)))))

The simple vector form [f5] works there for plain F-key bindings like ([f5] . my-command) (as suggested by @NickD in a comment above). Modifiers need slightly more complex syntax: ([(control f5)] . my-command).

1
  • f5 is still undefined
    – Brady Dean
    Commented Jul 28, 2021 at 23:31
0

If the goal is simply to bind a key in a given mode, use-package isn't the only road to Rome.

As long as the mode map variable is defined, define-key is also an option:

(define-key python-mode-map [f6] 'my-other-special-function)

If there is a need to test things out you might define an interactive function and use local-set-key.

;; Try invoking this once in python mode.
(defun bind-f5-in-python-mode ()
  (interactive)
  (local-set-key [f5] 'my-special-function))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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