How do I answer y automatically (kill-matching-buffers asks if I should kill a modified buffer)?

前端 未结 4 617
深忆病人
深忆病人 2021-01-12 05:28

In Emacs - how do I kill buffers matching regexp?

Edit:

How do I answer y automatically (kill-matching-buffers ask

相关标签:
4条回答
  • 2021-01-12 06:00
    M-x kill-matching-buffers
    

    This will prompt for a regex, check the documentation for details.

    0 讨论(0)
  • 2021-01-12 06:01

    You can use kill-matching-buffers. Below code effectively behaves as if kill-buffer (which does not ask before killing) was called instead of kill-buffer-ask:

    (defun kill-matching-buffers-just-do-it ()
      "Kill buffers whose names match REGEXP, without asking."
      (interactive)
      (cl-letf (((symbol-function 'kill-buffer-ask) #'kill-buffer))
        (call-interactively #'kill-matching-buffers)))
    
    0 讨论(0)
  • 2021-01-12 06:21

    For Emacs version >=24, the kmb.el library from ELPA repository does exactly that with the command kmb-kill-matching-buffers-no-ask.

    It also provides the command kmb-delete-process-and-kill-buffer-no-ask, which kills the current buffer (without confirmation).

    I bind the latter command as follows:

    (global-set-key (kbd "H-M-<delete>") 'kmb-delete-process-and-kill-buffer-no-ask)
    

    so that i don't call it accidentaly, just when i need it.

    0 讨论(0)
  • 2021-01-12 06:22

    How do I answer y automatically (kill-matching-buffers asks if I should kill a modified buffer)?

    kill-matching-buffers calls kill-buffer-ask which calls yes-or-no-p. You could temporarily redefine the latter, but for safety reasons I am inclined not to do that -- killing a given buffer could trigger other functionality which needs to ask a yes-or-no question.

    Redefining kill-buffer-ask seems a safer bet (or simply copying and modifying the kill-matching-buffers function itself).

    (require 'cl)
    (defun bk-kill-buffers (regexp)
      "Kill buffers matching REGEXP without asking for confirmation."
      (interactive "sKill buffers matching this regular expression: ")
      (flet ((kill-buffer-ask (buffer) (kill-buffer buffer)))
        (kill-matching-buffers regexp)))
    
    0 讨论(0)
提交回复
热议问题