1

I have the command kill-this-buffer bound to C-x C-x. yet it seems it doesn't work in all buffers (such as the calendar month view etc. yet the emacs menu close always does work. whats the difference between the two (the menu close doesn't seem to have a keybind) and how does one bind it to a key?

  • 2
    Might not be a good idea to bind C-x C-x to kill-buffer, which is by default bound to exchange-point-and-mark which is a very useful function. – Chakravarthy Raghunandan Feb 8 '17 at 6:57
4

In whatever buffer your keyboard shortcut does not work as expected, type C-h k or M-x describe-key and then your keyboard shortcut to see what function it is actually bound to -- major-modes and minor-modes can override global key bindings.

For example, in the *Calendar* buffer type C-h k C-x C-x and see that it is bound to calendar-exchange-point-and-mark, which is defined in the calendar-mode-map.

Some people like a global minor-mode for custom keybindings, whereas others may chose to unset / remove major-mode and minor-mode bindings that conflict with global-bindings. And others may choose to assign additional major-mode / minor-mode bindings for certain functions.

| improve this answer | |
1

The menu bar's close invokes kill-this-buffer. The problem, as @lawlist describes, is that your global C-x C-x binding is being overwritten by calendar-mode.

C-c <letter> key sequences are reserved for the user, so try a global binding such as C-c k instead.

Also note that kill-this-buffer is part of the menu-bar package (written long before the convention that all symbols of a package need to be prefixed by the package name was strictly enforced). The code is as below:

(defun kill-this-buffer ()  ; for the menu bar
  "Kill the current buffer.
When called in the minibuffer, get out of the minibuffer
using `abort-recursive-edit'."
  (interactive)
  (cond
   ;; Don't do anything when `menu-frame' is not alive or visible
   ;; (Bug#8184).
   ((not (menu-bar-menu-frame-live-and-visible-p)))
   ((menu-bar-non-minibuffer-window-p)
    (kill-buffer (current-buffer)))
   (t
    (abort-recursive-edit))))

I've experienced issues with it not working sometimes (presumably when (not (menu-bar-menu-frame-live-and-visible-p))), so I just bind C-c k to (lambda () (interactive) (kill-buffer (current-buffer))).

| improve this answer | |
  • Because kill-this-buffer might silently do nothing, you can use kill-current-buffer (emacs 26.3). – George Moutsopoulos Jan 28 at 13:05

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.