3

This is what I have:

(add-hook 'magit-status-mode-hook
  (lambda ()
    (local-set-key (kbd "<C-return>") 'magit-diff-visit-file-other-window)))

Opening a magit-status buffer fails with "C- must prefix a single character, not return".

There are several related questions, but their solutions (for example moving the <> around return instead of the whole string) don't work. Using RET instead of return doesn't work either.

Emacs 26.1

8
  • (local-set-key (kbd "C-<return>") #'magit-diff-visit-file-other-window) works fine for me. What happens when you try that?
    – Tyler
    Sep 12, 2019 at 16:02
  • Typo on the placement of <
    – Dan
    Sep 12, 2019 at 17:06
  • 1
    @Dan: Both of these return the same thing ([C-return]): (kbd "C-<return>") and (kbd "<C-return>").
    – Drew
    Sep 12, 2019 at 17:20
  • OP: Are you using Emacs in terminal mode instead of with a graphic display? Does your terminal support pseudo-function keys such as <return>? What does C-h k tell you when you hit the Return key?
    – Drew
    Sep 12, 2019 at 17:21
  • I get that error from (kbd "C-return") (i.e., no < at all), is it possible you have that expression somewhere else in your config?
    – npostavs
    Sep 12, 2019 at 23:08

2 Answers 2

3

In the comments we established that the problem wasn't the code shown in the question:

(add-hook 'magit-status-mode-hook
  (lambda ()
    (local-set-key (kbd "<C-return>") 'magit-diff-visit-file-other-window)))

The problem was the use of this pattern:

(add-hook 'HOOK (lambda ...))

When that pattern is used, updating the code of that (lambda...) function necessitates explicitly removing the original/bad lambda from the hook variable before adding a new updated version -- otherwise both functions will be called when the hook runs.

So the problem was that an earlier attempt (with a bad key binding syntax) was still being executed.

Inspecting C-hv magit-status-mode-hook showed that the old lambda was still present.


The better pattern is:

(add-hook 'magit-status-mode-hook 'my-magit-status-mode-hook)

(defun my-magit-status-mode-hook ()
  "Custom `magit-status-mode' behaviours."
  (local-set-key (kbd "<C-return>") 'magit-diff-visit-file-other-window))

As the named function can subsequently be updated without also needing to touch the hook variable.

1

I had this issue as well when I was trying to use C-<return> for another command in elpy. I managed to resolve it by first looking up the global key binding for C-<return> (try C-h k then type C-<return> to see what Emacs thinks is assigned to that key). In my situation, C-<return> was bound to CUA mode.

Then go into your configuration file and first set that binding to nil. In my situation, it looked like this:

;; Add keybindings for python that override CUA mode rect
(define-key cua-global-keymap [C-return] nil)
(global-set-key (kbd "C-RET") 'elpy-shell-send-statement-and-step)

Your Answer

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

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