Don’t kill-buffer, kill-this-buffer instead

Here’s a tiny tip. By default C-x k runs the command kill-buffer which prompts you for which buffer you want to kill, defaulting to the current active buffer. I don’t know about you, but I rarely want to kill a different buffer than the one I am looking at, so I rebind C-x k to kill-this-buffer which just kills the current buffer without prompting (unless there are unsaved changes).

Just add this to your emacs config file

(global-set-key (kbd "C-x k") 'kill-this-buffer)

Update 2017-04-24

Commenter thbit points out that using kill-this-buffer in this way is unstable (see discussion here). Here is a safer alternative.

(defun bjm/kill-this-buffer ()
  "Kill the current buffer."
  (kill-buffer (current-buffer)))

(global-set-key (kbd "C-x k") 'bjm/kill-this-buffer)
  • m31271n

    Nice Tip. Man.

  • simple, however super effective.

  • thanks!

  • Did you know that it is bound by default to Super-k?

    • Bart

      No it’s not.

      • Oh yes it is. Start a fresh emacs (emacs -q) then `C-h k` then `Super-k` and you’ll see this doc:

        s-k runs the command kill-this-buffer (found in global-map), which is an interactive compiled Lisp function in ‘menu-bar.el’.

        It is bound to s-k, s-&, .


        Kill the current buffer.

        When called in the minibuffer, get out of the minibuffer

        using ‘abort-recursive-edit’.

  • This is the kind of thing that is clearly better, but muscle memory might stop this from ever helping me. I’m going to try it out, but I’m expecting that for at least a month or two, I’m going to keep finding myself in a random directory after killing a buffer and instinctively hitting RET

  • thblt

    kill-this-buffer is meant to be called from the menu bar only, and may
    behave strangely or not at all, especially of the menu bar is not visible. See
    this reddit conversation for more explanation and an alternative:

    TLDR: bind to (lambda () (interactive) (kill-buffer (current-buffer))) instead of kill-this-buffer