0

I try to add a nicer key binding to kill the current buffer.

An obvious solution is to use kill-this-buffer, but often it does not work, or e.g. does not work the first time. The key binding for it is definitely not overridden by the buffers keymap, etc.

Indeed, the docs for kill-this-buffer say:

This command can be reliably invoked only from the menu bar, otherwise it could decide to silently do nothing.

This is exactly the observed behavior.

What could be a nice alternative? Is something like (kill-buffer (current-buffer)) going to work reliably instead? Am I missing any subtleties?

  • Good question!! – Drew Jan 17 at 19:14
  • 1
    I'd recommend you M-x report-emacs-bug requesting that kill-this-buffer be made to work in your use case. The current quirk is not a feature. – Stefan Jan 17 at 23:59
  • 1
    I would like to know the conditions under which kill-this-buffer supposedly does not work. I have bound kill-this buffer globally to C-q. I have used this command every day hundreds of times for years. I have never had a single instance where it proved unreliable and failed to kill the buffer I was in. – devcom Jan 18 at 4:36
1

Short answer: (kill-buffer (current-buffer)) works fine.

Longer answer: My init.el has

;; kill the current buffer instead of choosing
;; http://pragmaticemacs.com/emacs/dont-kill-buffer-kill-this-buffer-instead/
;; except that kill-this-buffer may break if menus are not available:
;; https://www.reddit.com/r/emacs/comments/64xb3q/killthisbuffer_sometimes_just_stops_working/
(bind-key "k" (lambda () (interactive) (kill-buffer (current-buffer))) ctl-x-map)

Check the links therein for more discussion.

1

You can use the function kill-current-buffer from simple.el. The docstring says:

kill-current-buffer is an interactive compiled Lisp function in ‘simple.el’.

(kill-current-buffer)

Kill the current buffer. When called in the minibuffer, get out of the minibuffer using ‘abort-recursive-edit’.

This is like ‘kill-this-buffer’, but it doesn’t have to be invoked via the menu bar, and pays no attention to the menu-bar’s frame.

-2

The fastest way is by a keyboard shortcut, which is C-x k.

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.