How to Kill buffer in emacs without answering confirmation?
This will kill the current visible buffer without confirmation unless the buffer has been modified. In this last case, you have to answer y/n.
(global-set-key [(control x) (k)] 'kill-this-buffer)
Use (kill-current-buffer)
instead of (kill-this-buffer)
if you want to bind it to some key. See the docs for (kill-this-buffer)
...
This command can be reliably invoked only from the menu bar, otherwise it could decide to silently do nothing.
and (kill-current-buffer)
...
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.
So I would put the following in my init.el
:
(global-set-key (kbd "C-x k") 'kill-current-buffer)
This works at least in emacs 26.1.
OK, I've done some poking around in the Emacs manual and found a working solution (as of Emacs 23.4.1). It's almost identical to Noufal's solution:
(defun kill-this-buffer-volatile ()
"Kill current buffer, even if it has been modified."
(interactive)
(set-buffer-modified-p nil)
(kill-this-buffer))
I've renamed the function a bit to make it a closer cousin to kill-this-buffer
.
Apparently, the EmacsWiki has a page on this topic at http://www.emacswiki.org/emacs/KillBufferUnconditionally (modified in 2007), but the code is just a copy of Noufal's.
I use this
(defun volatile-kill-buffer ()
"Kill current buffer unconditionally."
(interactive)
(let ((buffer-modified-p nil))
(kill-buffer (current-buffer))))
(global-set-key (kbd "C-x k") 'volatile-kill-buffer) ;; Unconditionally kill unmodified buffers.
It will kill the buffer unless it's modified.