开发者

How to Kill buffer in emacs without answering confirmation?

How to ki开发者_Python百科ll the buffer in emacs without being questioned.


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)


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.


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.


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.


I use the following piece of code -- unlike Noufal's solution of ignoring the buffer being modified or not, this will save the buffer and then kill it. It also deletes the window which makes a difference when you have several sub-windows showing -- by default it will remove the window instead of switching to some other buffer. (To use this conveniently, you need to bind some key to it, of course.)

;; Kill the current buffer immediatly, saving it if needed.
(defvar kill-save-buffer-delete-windows t
  "*Delete windows when `kill-save-buffer' is used.
If this is non-nil, then `kill-save-buffer' will also delete the corresponding
windows.  This is inverted by `kill-save-buffer' when called with a prefix.")
(defun kill-save-buffer (arg)
  "Save the current buffer (if needed) and then kill it.
Also, delete its windows according to `kill-save-buffer-delete-windows'.
A prefix argument ARG reverses this behavior."
  (interactive "P")
  (let ((del kill-save-buffer-delete-windows))
    (when arg (setq del (not del)))
    (when (and (buffer-file-name) (not (file-directory-p (buffer-file-name))))
      (save-buffer))
    (let ((buf (current-buffer)))
      (when del (delete-windows-on buf))
      (kill-buffer buf))))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜